<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module Enumerable - RDoc Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0-preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#module-Enumerable-label-What-27s+Here">What’s Here</a>
    <li><a href="#module-Enumerable-label-Methods+for+Querying">Methods for Querying</a>
    <li><a href="#module-Enumerable-label-Methods+for+Fetching">Methods for Fetching</a>
    <li><a href="#module-Enumerable-label-Methods+for+Searching+and+Filtering">Methods for Searching and Filtering</a>
    <li><a href="#module-Enumerable-label-Methods+for+Sorting">Methods for Sorting</a>
    <li><a href="#module-Enumerable-label-Methods+for+Iterating">Methods for Iterating</a>
    <li><a href="#module-Enumerable-label-Other+Methods">Other Methods</a>
    <li><a href="#module-Enumerable-label-Usage">Usage</a>
    <li><a href="#module-Enumerable-label-Enumerable+in+Ruby+Classes">Enumerable in Ruby Classes</a>
    <li><a href="#module-Enumerable-label-About+the+Examples">About the Examples</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-i-all-3F">#all?</a>
    <li ><a href="#method-i-any-3F">#any?</a>
    <li ><a href="#method-i-chain">#chain</a>
    <li ><a href="#method-i-chunk">#chunk</a>
    <li ><a href="#method-i-chunk_while">#chunk_while</a>
    <li ><a href="#method-i-collect">#collect</a>
    <li ><a href="#method-i-collect_concat">#collect_concat</a>
    <li ><a href="#method-i-compact">#compact</a>
    <li ><a href="#method-i-count">#count</a>
    <li ><a href="#method-i-cycle">#cycle</a>
    <li ><a href="#method-i-detect">#detect</a>
    <li ><a href="#method-i-drop">#drop</a>
    <li ><a href="#method-i-drop_while">#drop_while</a>
    <li ><a href="#method-i-each_cons">#each_cons</a>
    <li ><a href="#method-i-each_entry">#each_entry</a>
    <li ><a href="#method-i-each_slice">#each_slice</a>
    <li ><a href="#method-i-each_with_index">#each_with_index</a>
    <li ><a href="#method-i-each_with_object">#each_with_object</a>
    <li ><a href="#method-i-entries">#entries</a>
    <li ><a href="#method-i-filter">#filter</a>
    <li ><a href="#method-i-filter_map">#filter_map</a>
    <li ><a href="#method-i-find">#find</a>
    <li ><a href="#method-i-find_all">#find_all</a>
    <li ><a href="#method-i-find_index">#find_index</a>
    <li ><a href="#method-i-first">#first</a>
    <li ><a href="#method-i-flat_map">#flat_map</a>
    <li ><a href="#method-i-grep">#grep</a>
    <li ><a href="#method-i-grep_v">#grep_v</a>
    <li ><a href="#method-i-group_by">#group_by</a>
    <li ><a href="#method-i-include-3F">#include?</a>
    <li ><a href="#method-i-inject">#inject</a>
    <li ><a href="#method-i-lazy">#lazy</a>
    <li ><a href="#method-i-map">#map</a>
    <li ><a href="#method-i-max">#max</a>
    <li ><a href="#method-i-max_by">#max_by</a>
    <li ><a href="#method-i-member-3F">#member?</a>
    <li ><a href="#method-i-min">#min</a>
    <li ><a href="#method-i-min_by">#min_by</a>
    <li ><a href="#method-i-minmax">#minmax</a>
    <li ><a href="#method-i-minmax_by">#minmax_by</a>
    <li ><a href="#method-i-none-3F">#none?</a>
    <li ><a href="#method-i-one-3F">#one?</a>
    <li ><a href="#method-i-partition">#partition</a>
    <li ><a href="#method-i-reduce">#reduce</a>
    <li ><a href="#method-i-reject">#reject</a>
    <li ><a href="#method-i-reverse_each">#reverse_each</a>
    <li ><a href="#method-i-select">#select</a>
    <li ><a href="#method-i-slice_after">#slice_after</a>
    <li ><a href="#method-i-slice_before">#slice_before</a>
    <li ><a href="#method-i-slice_when">#slice_when</a>
    <li ><a href="#method-i-sort">#sort</a>
    <li ><a href="#method-i-sort_by">#sort_by</a>
    <li ><a href="#method-i-sum">#sum</a>
    <li ><a href="#method-i-take">#take</a>
    <li ><a href="#method-i-take_while">#take_while</a>
    <li ><a href="#method-i-tally">#tally</a>
    <li ><a href="#method-i-to_a">#to_a</a>
    <li ><a href="#method-i-to_h">#to_h</a>
    <li ><a href="#method-i-to_set">#to_set</a>
    <li ><a href="#method-i-uniq">#uniq</a>
    <li ><a href="#method-i-zip">#zip</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-Enumerable">
    <h1 id="module-Enumerable" class="module">
      module Enumerable
    </h1>

    <section class="description">
    
<h2 id="module-Enumerable-label-What-27s+Here">What’s Here<span><a href="#module-Enumerable-label-What-27s+Here">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Module Enumerable provides methods that are useful to a collection class for:</p>
<ul><li>
<p><a href="Enumerable.html#module-Enumerable-label-Methods+for+Querying">Querying</a></p>
</li><li>
<p><a href="Enumerable.html#module-Enumerable-label-Methods+for+Fetching">Fetching</a></p>
</li><li>
<p><a href="Enumerable.html#module-Enumerable-label-Methods+for+Searching+and+Filtering">Searching and Filtering</a></p>
</li><li>
<p><a href="Enumerable.html#module-Enumerable-label-Methods+for+Sorting">Sorting</a></p>
</li><li>
<p><a href="Enumerable.html#module-Enumerable-label-Methods+for+Iterating">Iterating</a></p>
</li><li>
<p><a href="Enumerable.html#module-Enumerable-label-Other+Methods">And more.…</a></p>
</li></ul>

<h3 id="module-Enumerable-label-Methods+for+Querying">Methods for Querying<span><a href="#module-Enumerable-label-Methods+for+Querying">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>These methods return information about the Enumerable other than the elements themselves:</p>
<ul><li>
<p><a href="Enumerable.html#method-i-include-3F"><code>include?</code></a>, <a href="Enumerable.html#method-i-member-3F"><code>member?</code></a>: Returns <code>true</code> if <code>self == object</code>, <code>false</code> otherwise.</p>
</li><li>
<p><a href="Enumerable.html#method-i-all-3F"><code>all?</code></a>: Returns <code>true</code> if all elements meet a specified criterion; <code>false</code> otherwise.</p>
</li><li>
<p><a href="Enumerable.html#method-i-any-3F"><code>any?</code></a>: Returns <code>true</code> if any element meets a specified criterion; <code>false</code> otherwise.</p>
</li><li>
<p><a href="Enumerable.html#method-i-none-3F"><code>none?</code></a>: Returns <code>true</code> if no element meets a specified criterion; <code>false</code> otherwise.</p>
</li><li>
<p><a href="Enumerable.html#method-i-one-3F"><code>one?</code></a>: Returns <code>true</code> if exactly one element meets a specified criterion; <code>false</code> otherwise.</p>
</li><li>
<p><a href="Enumerable.html#method-i-count"><code>count</code></a>: Returns the count of elements, based on an argument or block criterion, if given.</p>
</li><li>
<p><a href="Enumerable.html#method-i-tally"><code>tally</code></a>: Returns a new <a href="Hash.html"><code>Hash</code></a> containing the counts of occurrences of each element.</p>
</li></ul>

<h3 id="module-Enumerable-label-Methods+for+Fetching">Methods for Fetching<span><a href="#module-Enumerable-label-Methods+for+Fetching">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>These methods return entries from the Enumerable, without modifying it:</p>

<p><em>Leading, trailing, or all elements</em>:</p>
<ul><li>
<p><a href="Enumerable.html#method-i-entries"><code>entries</code></a>, <a href="Enumerable.html#method-i-to_a"><code>to_a</code></a>: Returns all elements.</p>
</li><li>
<p><a href="Enumerable.html#method-i-first"><code>first</code></a>: Returns the first element or leading elements.</p>
</li><li>
<p><a href="Enumerable.html#method-i-take"><code>take</code></a>: Returns a specified number of leading elements.</p>
</li><li>
<p><a href="Enumerable.html#method-i-drop"><code>drop</code></a>: Returns a specified number of trailing elements.</p>
</li><li>
<p><a href="Enumerable.html#method-i-take_while"><code>take_while</code></a>: Returns leading elements as specified by the given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-drop_while"><code>drop_while</code></a>: Returns trailing elements as specified by the given block.</p>
</li></ul>

<p><em>Minimum and maximum value elements</em>:</p>
<ul><li>
<p><a href="Enumerable.html#method-i-min"><code>min</code></a>: Returns the elements whose values are smallest among the elements, as determined by <code>&lt;=&gt;</code> or a given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-max"><code>max</code></a>: Returns the elements whose values are largest among the elements, as determined by <code>&lt;=&gt;</code> or a given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-minmax"><code>minmax</code></a>: Returns a 2-element <a href="Array.html"><code>Array</code></a> containing the smallest and largest elements.</p>
</li><li>
<p><a href="Enumerable.html#method-i-min_by"><code>min_by</code></a>: Returns the smallest element, as determined by the given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-max_by"><code>max_by</code></a>: Returns the largest element, as determined by the given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-minmax_by"><code>minmax_by</code></a>: Returns the smallest and largest elements, as determined by the given block.</p>
</li></ul>

<p><em>Groups, slices, and partitions</em>:</p>
<ul><li>
<p><a href="Enumerable.html#method-i-group_by"><code>group_by</code></a>: Returns a <a href="Hash.html"><code>Hash</code></a> that partitions the elements into groups.</p>
</li><li>
<p><a href="Enumerable.html#method-i-partition"><code>partition</code></a>: Returns elements partitioned into two new Arrays, as determined by the given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-slice_after"><code>slice_after</code></a>: Returns a new <a href="Enumerator.html"><code>Enumerator</code></a> whose entries are a partition of <code>self</code>, based either on a given <code>object</code> or a given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-slice_before"><code>slice_before</code></a>: Returns a new <a href="Enumerator.html"><code>Enumerator</code></a> whose entries are a partition of <code>self</code>, based either on a given <code>object</code> or a given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-slice_when"><code>slice_when</code></a>: Returns a new <a href="Enumerator.html"><code>Enumerator</code></a> whose entries are a partition of <code>self</code> based on the given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-chunk"><code>chunk</code></a>: Returns elements organized into chunks as specified by the given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-chunk_while"><code>chunk_while</code></a>: Returns elements organized into chunks as specified by the given block.</p>
</li></ul>

<h3 id="module-Enumerable-label-Methods+for+Searching+and+Filtering">Methods for Searching and Filtering<span><a href="#module-Enumerable-label-Methods+for+Searching+and+Filtering">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>These methods return elements that meet a specified criterion:</p>
<ul><li>
<p><a href="Enumerable.html#method-i-find"><code>find</code></a>, <a href="Enumerable.html#method-i-detect"><code>detect</code></a>: Returns an element selected by the block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-find_all"><code>find_all</code></a>, <a href="Enumerable.html#method-i-filter"><code>filter</code></a>, <a href="Enumerable.html#method-i-select"><code>select</code></a>: Returns elements selected by the block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-find_index"><code>find_index</code></a>: Returns the index of an element selected by a given object or block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-reject"><code>reject</code></a>: Returns elements not rejected by the block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-uniq"><code>uniq</code></a>: Returns elements that are not duplicates.</p>
</li></ul>

<h3 id="module-Enumerable-label-Methods+for+Sorting">Methods for Sorting<span><a href="#module-Enumerable-label-Methods+for+Sorting">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>These methods return elements in sorted order:</p>
<ul><li>
<p><a href="Enumerable.html#method-i-sort"><code>sort</code></a>: Returns the elements, sorted by <code>&lt;=&gt;</code> or the given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-sort_by"><code>sort_by</code></a>: Returns the elements, sorted by the given block.</p>
</li></ul>

<h3 id="module-Enumerable-label-Methods+for+Iterating">Methods for Iterating<span><a href="#module-Enumerable-label-Methods+for+Iterating">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><a href="Enumerable.html#method-i-each_entry"><code>each_entry</code></a>: Calls the block with each successive element (slightly different from each).</p>
</li><li>
<p><a href="Enumerable.html#method-i-each_with_index"><code>each_with_index</code></a>: Calls the block with each successive element and its index.</p>
</li><li>
<p><a href="Enumerable.html#method-i-each_with_object"><code>each_with_object</code></a>: Calls the block with each successive element and a given object.</p>
</li><li>
<p><a href="Enumerable.html#method-i-each_slice"><code>each_slice</code></a>: Calls the block with successive non-overlapping slices.</p>
</li><li>
<p><a href="Enumerable.html#method-i-each_cons"><code>each_cons</code></a>: Calls the block with successive overlapping slices. (different from <a href="Enumerable.html#method-i-each_slice"><code>each_slice</code></a>).</p>
</li><li>
<p><a href="Enumerable.html#method-i-reverse_each"><code>reverse_each</code></a>: Calls the block with each successive element, in reverse order.</p>
</li></ul>

<h3 id="module-Enumerable-label-Other+Methods">Other Methods<span><a href="#module-Enumerable-label-Other+Methods">&para;</a> <a href="#top">&uarr;</a></span></h3>
<ul><li>
<p><a href="Enumerable.html#method-i-map"><code>map</code></a>, <a href="Enumerable.html#method-i-collect"><code>collect</code></a>: Returns objects returned by the block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-filter_map"><code>filter_map</code></a>: Returns truthy objects returned by the block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-flat_map"><code>flat_map</code></a>, <a href="Enumerable.html#method-i-collect_concat"><code>collect_concat</code></a>: Returns flattened objects returned by the block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-grep"><code>grep</code></a>: Returns elements selected by a given object or objects returned by a given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-grep_v"><code>grep_v</code></a>: Returns elements selected by a given object or objects returned by a given block.</p>
</li><li>
<p><a href="Enumerable.html#method-i-reduce"><code>reduce</code></a>, <a href="Enumerable.html#method-i-inject"><code>inject</code></a>: Returns the object formed by combining all elements.</p>
</li><li>
<p><a href="Enumerable.html#method-i-sum"><code>sum</code></a>: Returns the sum of the elements, using method <code>+</code>.</p>
</li><li>
<p><a href="Enumerable.html#method-i-zip"><code>zip</code></a>: Combines each element with elements from other enumerables; returns the n-tuples or calls the block with each.</p>
</li><li>
<p><a href="Enumerable.html#method-i-cycle"><code>cycle</code></a>: Calls the block with each element, cycling repeatedly.</p>
</li></ul>

<h2 id="module-Enumerable-label-Usage">Usage<span><a href="#module-Enumerable-label-Usage">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>To use module Enumerable in a collection class:</p>
<ul><li>
<p>Include it:</p>

<pre class="ruby"><span class="ruby-identifier">include</span> <span class="ruby-constant">Enumerable</span>
</pre>
</li><li>
<p>Implement method <code>#each</code> which must yield successive elements of the collection. The method will be called by almost any Enumerable method.</p>
</li></ul>

<p>Example:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Foo</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Enumerable</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">each</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span>
    <span class="ruby-keyword">yield</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-constant">Foo</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">each_entry</span>{ <span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">element</span> }
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-value">1</span>
[<span class="ruby-value">1</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">nil</span>
</pre>

<h2 id="module-Enumerable-label-Enumerable+in+Ruby+Classes">Enumerable in Ruby Classes<span><a href="#module-Enumerable-label-Enumerable+in+Ruby+Classes">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>These Ruby core classes include (or extend) Enumerable:</p>
<ul><li>
<p><a href="ARGF.html"><code>ARGF</code></a></p>
</li><li>
<p><a href="Array.html"><code>Array</code></a></p>
</li><li>
<p><a href="Dir.html"><code>Dir</code></a></p>
</li><li>
<p><a href="Enumerator.html"><code>Enumerator</code></a></p>
</li><li>
<p><a href="ENV.html"><code>ENV</code></a> (extends)</p>
</li><li>
<p><a href="Hash.html"><code>Hash</code></a></p>
</li><li>
<p><a href="IO.html"><code>IO</code></a></p>
</li><li>
<p><a href="Range.html"><code>Range</code></a></p>
</li><li>
<p><a href="Struct.html"><code>Struct</code></a></p>
</li></ul>

<p>These Ruby standard library classes include Enumerable:</p>
<ul><li>
<p>CSV</p>
</li><li>
<p>CSV::Table</p>
</li><li>
<p>CSV::Row</p>
</li><li>
<p>Set</p>
</li></ul>

<p>Virtually all methods in Enumerable call method <code>#each</code> in the including class:</p>
<ul><li>
<p><code>Hash#each</code> yields the next key-value pair as a 2-element <a href="Array.html"><code>Array</code></a>.</p>
</li><li>
<p><code>Struct#each</code> yields the next name-value pair as a 2-element <a href="Array.html"><code>Array</code></a>.</p>
</li><li>
<p>For the other classes above, <code>#each</code> yields the next object from the collection.</p>
</li></ul>

<h2 id="module-Enumerable-label-About+the+Examples">About the Examples<span><a href="#module-Enumerable-label-About+the+Examples">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>The example code snippets for the Enumerable methods:</p>
<ul><li>
<p>Always show the use of one or more Array-like classes (often <a href="Array.html"><code>Array</code></a> itself).</p>
</li><li>
<p>Sometimes show the use of a Hash-like class. For some methods, though, the usage would not make sense, and so it is not shown.  Example: <a href="Enumerable.html#method-i-tally"><code>tally</code></a> would find exactly one of each <a href="Hash.html"><code>Hash</code></a> entry.</p>
</li></ul>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">





                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-all-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          all?                  &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          all?(pattern)         &rarr; true or false
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          all? {|element| ... } &rarr; true or false
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns whether every element meets a given criterion.</p>

<p>If <code>self</code> has no element, returns <code>true</code> and argument or block are not used.</p>

<p>With no argument and no block, returns whether every element is truthy:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">all?</span>           <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">all?</span>      <span class="ruby-comment"># =&gt; true</span>
[<span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-keyword">nil</span>].<span class="ruby-identifier">all?</span>      <span class="ruby-comment"># =&gt; false</span>
[<span class="ruby-string">&#39;a&#39;</span>,<span class="ruby-string">&#39;b&#39;</span>, <span class="ruby-keyword">false</span>].<span class="ruby-identifier">all?</span> <span class="ruby-comment"># =&gt; false</span>
[].<span class="ruby-identifier">all?</span>               <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>With argument <code>pattern</code> and no block, returns whether for each element <code>element</code>, <code>pattern === element</code>:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">all?</span>(<span class="ruby-constant">Integer</span>)                 <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">all?</span>(<span class="ruby-constant">Numeric</span>)                 <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">all?</span>(<span class="ruby-constant">Float</span>)                   <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">all?</span>(<span class="ruby-regexp">/ba/</span>)       <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">all?</span>(<span class="ruby-regexp">/bar/</span>)      <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">all?</span>(<span class="ruby-string">&#39;ba&#39;</span>)       <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">all?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">all?</span>(<span class="ruby-constant">Hash</span>)  <span class="ruby-comment"># =&gt; false</span>
[].<span class="ruby-identifier">all?</span>(<span class="ruby-constant">Integer</span>)                     <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>With a block given, returns whether the block returns a truthy value for every element:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">all?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">5</span> }                    <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">all?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">4</span> }                    <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">all?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">3</span> } <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">all?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> } <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-any-3F"><code>any?</code></a>, <a href="Enumerable.html#method-i-none-3F"><code>none?</code></a> <a href="Enumerable.html#method-i-one-3F"><code>one?</code></a>.</p>

                              <div class="method-source-code" id="all-3F-source">
            <pre>static VALUE
enum_all(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo = MEMO_ENUM_NEW(Qtrue);
    WARN_UNUSED_BLOCK(argc);
    rb_block_call(obj, id_each, 0, 0, ENUMFUNC(all), (VALUE)memo);
    return memo-&gt;v1;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-any-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          any?                  &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          any?(pattern)         &rarr; true or false
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          any? {|element| ... } &rarr; true or false
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns whether any element meets a given criterion.</p>

<p>If <code>self</code> has no element, returns <code>false</code> and argument or block are not used.</p>

<p>With no argument and no block, returns whether any element is truthy:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">any?</span>          <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">any?</span>     <span class="ruby-comment"># =&gt; true</span>
[<span class="ruby-value">1</span>, <span class="ruby-keyword">false</span>, <span class="ruby-keyword">nil</span>].<span class="ruby-identifier">any?</span> <span class="ruby-comment"># =&gt; true</span>
[].<span class="ruby-identifier">any?</span>              <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>With argument <code>pattern</code> and no block, returns whether for any element <code>element</code>, <code>pattern === element</code>:</p>

<pre class="ruby">[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-value">0</span>].<span class="ruby-identifier">any?</span>(<span class="ruby-constant">Integer</span>)        <span class="ruby-comment"># =&gt; true</span>
[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-value">0</span>].<span class="ruby-identifier">any?</span>(<span class="ruby-constant">Numeric</span>)        <span class="ruby-comment"># =&gt; true</span>
[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-value">0</span>].<span class="ruby-identifier">any?</span>(<span class="ruby-constant">Float</span>)          <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">any?</span>(<span class="ruby-regexp">/m/</span>)        <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">any?</span>(<span class="ruby-regexp">/foo/</span>)      <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">any?</span>(<span class="ruby-string">&#39;ba&#39;</span>)       <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">any?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">any?</span>(<span class="ruby-constant">Hash</span>)  <span class="ruby-comment"># =&gt; false</span>
[].<span class="ruby-identifier">any?</span>(<span class="ruby-constant">Integer</span>)                     <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>With a block given, returns whether the block returns a truthy value for any element:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">any?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> }                    <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">any?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1</span> }                    <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">any?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1</span> } <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">any?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span> } <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-all-3F"><code>all?</code></a>, <a href="Enumerable.html#method-i-none-3F"><code>none?</code></a>, <a href="Enumerable.html#method-i-one-3F"><code>one?</code></a>.</p>

                              <div class="method-source-code" id="any-3F-source">
            <pre>static VALUE
enum_any(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo = MEMO_ENUM_NEW(Qfalse);
    WARN_UNUSED_BLOCK(argc);
    rb_block_call(obj, id_each, 0, 0, ENUMFUNC(any), (VALUE)memo);
    return memo-&gt;v1;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-chain" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          chain(*enums) &rarr; enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an enumerator object generated from this enumerator and given enumerables.</p>

<pre class="ruby"><span class="ruby-identifier">e</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">3</span>).<span class="ruby-identifier">chain</span>([<span class="ruby-value">4</span>, <span class="ruby-value">5</span>])
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment">#=&gt; [1, 2, 3, 4, 5]</span>
</pre>

                              <div class="method-source-code" id="chain-source">
            <pre>static VALUE
enum_chain(int argc, VALUE *argv, VALUE obj)
{
    VALUE enums = rb_ary_new_from_values(1, &amp;obj);
    rb_ary_cat(enums, argv, argc);
    return new_enum_chain(enums);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-chunk" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          chunk {|array| ... } &rarr; enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Each element in the returned enumerator is a 2-element array consisting of:</p>
<ul><li>
<p>A value returned by the block.</p>
</li><li>
<p>An array (“chunk”) containing the element for which that value was returned, and all following elements for which the block returned the same value:</p>
</li></ul>

<p>So that:</p>
<ul><li>
<p>Each block return value that is different from its predecessor begins a new chunk.</p>
</li><li>
<p>Each block return value that is the same as its predecessor continues the same chunk.</p>
</li></ul>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">e</span> = (<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">10</span>).<span class="ruby-identifier">chunk</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> (<span class="ruby-identifier">i</span> <span class="ruby-operator">/</span> <span class="ruby-value">3</span>).<span class="ruby-identifier">floor</span> } <span class="ruby-comment"># =&gt; #&lt;Enumerator: ...&gt;</span>
<span class="ruby-comment"># The enumerator elements.</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span> <span class="ruby-comment"># =&gt; [0, [0, 1, 2]]</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span> <span class="ruby-comment"># =&gt; [1, [3, 4, 5]]</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span> <span class="ruby-comment"># =&gt; [2, [6, 7, 8]]</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">next</span> <span class="ruby-comment"># =&gt; [3, [9, 10]]</span>
</pre>

<p>Method <code>chunk</code> is especially useful for an enumerable that is already sorted. This example counts words for each initial letter in a large array of words:</p>

<pre class="ruby"><span class="ruby-comment"># Get sorted words from a web page.</span>
<span class="ruby-identifier">url</span> = <span class="ruby-string">&#39;https://raw.githubusercontent.com/eneko/data-repository/master/data/words.txt&#39;</span>
<span class="ruby-identifier">words</span> = <span class="ruby-constant">URI</span><span class="ruby-operator">::</span><span class="ruby-identifier">open</span>(<span class="ruby-identifier">url</span>).<span class="ruby-identifier">readlines</span>
<span class="ruby-comment"># Make chunks, one for each letter.</span>
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">words</span>.<span class="ruby-identifier">chunk</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">word</span><span class="ruby-operator">|</span> <span class="ruby-identifier">word</span>.<span class="ruby-identifier">upcase</span>[<span class="ruby-value">0</span>] } <span class="ruby-comment"># =&gt; #&lt;Enumerator: ...&gt;</span>
<span class="ruby-comment"># Display &#39;A&#39; through &#39;F&#39;.</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">c</span>, <span class="ruby-identifier">words</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> [<span class="ruby-identifier">c</span>, <span class="ruby-identifier">words</span>.<span class="ruby-identifier">length</span>]; <span class="ruby-keyword">break</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">==</span> <span class="ruby-string">&#39;F&#39;</span> }
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;A&quot;</span>, <span class="ruby-value">17096</span>]
[<span class="ruby-string">&quot;B&quot;</span>, <span class="ruby-value">11070</span>]
[<span class="ruby-string">&quot;C&quot;</span>, <span class="ruby-value">19901</span>]
[<span class="ruby-string">&quot;D&quot;</span>, <span class="ruby-value">10896</span>]
[<span class="ruby-string">&quot;E&quot;</span>, <span class="ruby-value">8736</span>]
[<span class="ruby-string">&quot;F&quot;</span>, <span class="ruby-value">6860</span>]
</pre>

<p>You can use the special symbol <code>:_alone</code> to force an element into its own separate chuck:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">1</span>]
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">chunk</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">even?</span> <span class="ruby-operator">?</span> <span class="ruby-value">:_alone</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">true</span> }
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment"># =&gt; [[:_alone, [0]], [:_alone, [0]], [true, [1, 1]]]</span>
</pre>

<p>For example, you can put each line that contains a URL into its own chunk:</p>

<pre class="ruby"><span class="ruby-identifier">pattern</span> = <span class="ruby-regexp">/http/</span>
<span class="ruby-identifier">open</span>(<span class="ruby-identifier">filename</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">f</span>.<span class="ruby-identifier">chunk</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> <span class="ruby-identifier">line</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">pattern</span> <span class="ruby-operator">?</span> <span class="ruby-value">:_alone</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">true</span> }.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">lines</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">pp</span> <span class="ruby-identifier">lines</span>
  }
}
</pre>

<p>You can use the special symbol <code>:_separator</code> or <code>nil</code> to force an element to be ignored (not included in any chunk):</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">0</span>, <span class="ruby-value">-1</span>, <span class="ruby-value">1</span>, <span class="ruby-value">1</span>]
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">chunk</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-value">:_separator</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">true</span> }
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment"># =&gt; [[true, [0, 0]], [true, [1, 1]]]</span>
</pre>

<p>Note that the separator does end the chunk:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">0</span>, <span class="ruby-value">-1</span>, <span class="ruby-value">1</span>, <span class="ruby-value">-1</span>, <span class="ruby-value">1</span>]
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">chunk</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span> <span class="ruby-operator">?</span> <span class="ruby-value">:_separator</span> <span class="ruby-operator">:</span> <span class="ruby-keyword">true</span> }
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment"># =&gt; [[true, [0, 0]], [true, [1]], [true, [1]]]</span>
</pre>

<p>For example, the sequence of hyphens in svn log can be eliminated as follows:</p>

<pre class="ruby"><span class="ruby-identifier">sep</span> = <span class="ruby-string">&quot;-&quot;</span><span class="ruby-operator">*</span><span class="ruby-value">72</span> <span class="ruby-operator">+</span> <span class="ruby-string">&quot;\n&quot;</span>
<span class="ruby-constant">IO</span>.<span class="ruby-identifier">popen</span>(<span class="ruby-string">&quot;svn log README&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">f</span>.<span class="ruby-identifier">chunk</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">line</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">sep</span> <span class="ruby-operator">||</span> <span class="ruby-keyword">nil</span>
  }.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">_</span>, <span class="ruby-identifier">lines</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">pp</span> <span class="ruby-identifier">lines</span>
  }
}
<span class="ruby-comment">#=&gt; [&quot;r20018 | knu | 2008-10-29 13:20:42 +0900 (Wed, 29 Oct 2008) | 2 lines\n&quot;,</span>
<span class="ruby-comment">#    &quot;\n&quot;,</span>
<span class="ruby-comment">#    &quot;* README, README.ja: Update the portability section.\n&quot;,</span>
<span class="ruby-comment">#    &quot;\n&quot;]</span>
<span class="ruby-comment">#   [&quot;r16725 | knu | 2008-05-31 23:34:23 +0900 (Sat, 31 May 2008) | 2 lines\n&quot;,</span>
<span class="ruby-comment">#    &quot;\n&quot;,</span>
<span class="ruby-comment">#    &quot;* README, README.ja: Add a note about default C flags.\n&quot;,</span>
<span class="ruby-comment">#    &quot;\n&quot;]</span>
<span class="ruby-comment">#   ...</span>
</pre>

<p>Paragraphs separated by empty lines can be parsed as follows:</p>

<pre class="ruby"><span class="ruby-constant">File</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-string">&quot;README&quot;</span>).<span class="ruby-identifier">chunk</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
  <span class="ruby-regexp">/\A\s*\z/</span> <span class="ruby-operator">!~</span> <span class="ruby-identifier">line</span> <span class="ruby-operator">||</span> <span class="ruby-keyword">nil</span>
}.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">_</span>, <span class="ruby-identifier">lines</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">pp</span> <span class="ruby-identifier">lines</span>
}
</pre>

                              <div class="method-source-code" id="chunk-source">
            <pre>static VALUE
enum_chunk(VALUE enumerable)
{
    VALUE enumerator;

    RETURN_SIZED_ENUMERATOR(enumerable, 0, 0, enum_size);

    enumerator = rb_obj_alloc(rb_cEnumerator);
    rb_ivar_set(enumerator, id_chunk_enumerable, enumerable);
    rb_ivar_set(enumerator, id_chunk_categorize, rb_block_proc());
    rb_block_call(enumerator, idInitialize, 0, 0, chunk_i, enumerator);
    return enumerator;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-chunk_while" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          chunk_while {|elt_before, elt_after| bool } &rarr; an_enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates an enumerator for each chunked elements. The beginnings of chunks are defined by the block.</p>

<p>This method splits each chunk using adjacent elements, <em>elt_before</em> and <em>elt_after</em>, in the receiver enumerator. This method split chunks between <em>elt_before</em> and <em>elt_after</em> where the block returns <code>false</code>.</p>

<p>The block is called the length of the receiver enumerator minus one.</p>

<p>The result enumerator yields the chunked elements as an array. So <code>each</code> method can be called as follows:</p>

<pre>enum.chunk_while { |elt_before, elt_after| bool }.each { |ary| ... }</pre>

<p>Other methods of the <a href="Enumerator.html"><code>Enumerator</code></a> class and <a href="Enumerable.html"><code>Enumerable</code></a> module, such as <code>to_a</code>, <code>map</code>, etc., are also usable.</p>

<p>For example, one-by-one increasing subsequence can be chunked as follows:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">4</span>,<span class="ruby-value">9</span>,<span class="ruby-value">10</span>,<span class="ruby-value">11</span>,<span class="ruby-value">12</span>,<span class="ruby-value">15</span>,<span class="ruby-value">16</span>,<span class="ruby-value">19</span>,<span class="ruby-value">20</span>,<span class="ruby-value">21</span>]
<span class="ruby-identifier">b</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">chunk_while</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">j</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span><span class="ruby-value">+1</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">j</span> }
<span class="ruby-identifier">p</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment">#=&gt; [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]</span>
<span class="ruby-identifier">c</span> = <span class="ruby-identifier">b</span>.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">3</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">:</span> <span class="ruby-node">&quot;#{a.first}-#{a.last}&quot;</span> }
<span class="ruby-identifier">p</span> <span class="ruby-identifier">c</span> <span class="ruby-comment">#=&gt; [[1, 2], [4], &quot;9-12&quot;, [15, 16], &quot;19-21&quot;]</span>
<span class="ruby-identifier">d</span> = <span class="ruby-identifier">c</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;,&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">d</span> <span class="ruby-comment">#=&gt; &quot;1,2,4,9-12,15,16,19-21&quot;</span>
</pre>

<p>Increasing (non-decreasing) subsequence can be chunked as follows:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">9</span>, <span class="ruby-value">2</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">2</span>, <span class="ruby-value">7</span>, <span class="ruby-value">5</span>, <span class="ruby-value">9</span>, <span class="ruby-value">5</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">chunk_while</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">j</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-identifier">j</span> }.<span class="ruby-identifier">to_a</span>
<span class="ruby-comment">#=&gt; [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]</span>
</pre>

<p>Adjacent evens and odds can be chunked as follows: (<a href="Enumerable.html#method-i-chunk"><code>Enumerable#chunk</code></a> is another way to do it.)</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">7</span>, <span class="ruby-value">5</span>, <span class="ruby-value">9</span>, <span class="ruby-value">2</span>, <span class="ruby-value">0</span>, <span class="ruby-value">7</span>, <span class="ruby-value">9</span>, <span class="ruby-value">4</span>, <span class="ruby-value">2</span>, <span class="ruby-value">0</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">chunk_while</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">j</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">even?</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">j</span>.<span class="ruby-identifier">even?</span> }.<span class="ruby-identifier">to_a</span>
<span class="ruby-comment">#=&gt; [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]</span>
</pre>

<p><a href="Enumerable.html#method-i-slice_when"><code>Enumerable#slice_when</code></a> does the same, except splitting when the block returns <code>true</code> instead of <code>false</code>.</p>

                              <div class="method-source-code" id="chunk_while-source">
            <pre>static VALUE
enum_chunk_while(VALUE enumerable)
{
    VALUE enumerator;
    VALUE pred;

    pred = rb_block_proc();

    enumerator = rb_obj_alloc(rb_cEnumerator);
    rb_ivar_set(enumerator, id_slicewhen_enum, enumerable);
    rb_ivar_set(enumerator, id_slicewhen_pred, pred);
    rb_ivar_set(enumerator, id_slicewhen_inverted, Qtrue);

    rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
    return enumerator;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-collect" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">collect</span><span
                                class="method-args"> -&gt; enumerator</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of objects returned by the block.</p>

<p>With a block given, calls the block with successive elements; returns an array of the objects returned by the block:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span><span class="ruby-operator">*</span><span class="ruby-identifier">i</span> }                               <span class="ruby-comment"># =&gt; [0, 1, 4, 9, 16]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span><span class="ruby-operator">*</span><span class="ruby-value">2</span>} <span class="ruby-comment"># =&gt; [0, 2, 4]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="collect-source">
            <pre>static VALUE
enum_collect(VALUE obj)
{
    VALUE ary;
    int min_argc, max_argc;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    ary = rb_ary_new();
    min_argc = rb_block_min_max_arity(&amp;max_argc);
    rb_lambda_call(obj, id_each, 0, 0, collect_i, min_argc, max_argc, ary);

    return ary;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerable.html#method-i-map">map</a>
                            </div>

                          </div>

                  <div id="method-i-collect_concat" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">collect_concat</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of flattened objects returned by the block.</p>

<p>With a block given, calls the block with successive elements; returns a flattened array of objects returned by the block:</p>

<pre class="ruby">[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }                    <span class="ruby-comment"># =&gt; [0, -1, -2, -3]</span>
[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">element</span>, <span class="ruby-operator">-</span><span class="ruby-identifier">element</span>] }         <span class="ruby-comment"># =&gt; [0, 0, 1, -1, 2, -2, 3, -3]</span>
[[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>], [<span class="ruby-value">2</span>, <span class="ruby-value">3</span>]].<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span> <span class="ruby-operator">+</span> [<span class="ruby-value">100</span>] }                     <span class="ruby-comment"># =&gt; [0, 1, 100, 2, 3, 100]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>] } <span class="ruby-comment"># =&gt; [:foo, 0, :bar, 1, :baz, 2]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>Alias: <a href="Enumerable.html#method-i-collect_concat"><code>collect_concat</code></a>.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-flat_map">flat_map</a>
                            </div>
                          </div>

                  <div id="method-i-compact" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          compact &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of all non-<code>nil</code> elements:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-keyword">nil</span>, <span class="ruby-value">0</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-keyword">false</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-value">0</span>, <span class="ruby-keyword">nil</span>]
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">compact</span> <span class="ruby-comment"># =&gt; [0, &quot;a&quot;, false, false, &quot;a&quot;, 0]</span>
</pre>

                              <div class="method-source-code" id="compact-source">
            <pre>static VALUE
enum_compact(VALUE obj)
{
    VALUE ary;

    ary = rb_ary_new();
    rb_block_call(obj, id_each, 0, 0, compact_i, ary);

    return ary;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-count" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          count &rarr; integer
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          count(object) &rarr; integer
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          count {|element| ... } &rarr; integer
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the count of elements, based on an argument or block criterion, if given.</p>

<p>With no argument and no block given, returns the number of elements:</p>

<pre class="ruby">[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>].<span class="ruby-identifier">count</span>                <span class="ruby-comment"># =&gt; 3</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">count</span> <span class="ruby-comment"># =&gt; 3</span>
</pre>

<p>With argument <code>object</code> given, returns the number of elements that are <code>==</code> to <code>object</code>:</p>

<pre class="ruby">[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">1</span>].<span class="ruby-identifier">count</span>(<span class="ruby-value">1</span>)           <span class="ruby-comment"># =&gt; 2</span>
</pre>

<p>With a block given, calls the block with each element and returns the number of elements for which the block returns a truthy value:</p>

<pre class="ruby">[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">count</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span>}              <span class="ruby-comment"># =&gt; 2</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">count</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span>} <span class="ruby-comment"># =&gt; 2</span>
</pre>

                              <div class="method-source-code" id="count-source">
            <pre>static VALUE
enum_count(int argc, VALUE *argv, VALUE obj)
{
    VALUE item = Qnil;
    struct MEMO *memo;
    rb_block_call_func *func;

    if (argc == 0) {
        if (rb_block_given_p()) {
            func = count_iter_i;
        }
        else {
            func = count_all_i;
        }
    }
    else {
        rb_scan_args(argc, argv, &quot;1&quot;, &amp;item);
        if (rb_block_given_p()) {
            rb_warn(&quot;given block not used&quot;);
        }
        func = count_i;
    }

    memo = MEMO_NEW(item, 0, 0);
    rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
    return imemo_count_value(memo);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-cycle" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          cycle(n = nil) {|element| ...} &rarr;  nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          cycle(n = nil)                 &rarr;  enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>When called with positive integer argument <code>n</code> and a block, calls the block with each element, then does so again, until it has done so <code>n</code> times; returns <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = []
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">cycle</span>(<span class="ruby-value">3</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) } <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]</span>
<span class="ruby-identifier">a</span> = []
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">cycle</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]</span>
<span class="ruby-identifier">a</span> = []
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">cycle</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1], [:baz, 2], [:foo, 0], [:bar, 1], [:baz, 2]]</span>
</pre>

<p>If count is zero or negative, does not call the block.</p>

<p>When called with a block and <code>n</code> is <code>nil</code>, cycles forever.</p>

<p>When no block is given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="cycle-source">
            <pre>static VALUE
enum_cycle(int argc, VALUE *argv, VALUE obj)
{
    VALUE ary;
    VALUE nv = Qnil;
    long n, i, len;

    rb_check_arity(argc, 0, 1);

    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_cycle_size);
    if (!argc || NIL_P(nv = argv[0])) {
        n = -1;
    }
    else {
        n = NUM2LONG(nv);
        if (n &lt;= 0) return Qnil;
    }
    ary = rb_ary_new();
    RBASIC_CLEAR_CLASS(ary);
    rb_block_call(obj, id_each, 0, 0, cycle_i, ary);
    len = RARRAY_LEN(ary);
    if (len == 0) return Qnil;
    while (n &lt; 0 || 0 &lt; --n) {
        for (i=0; i&lt;len; i++) {
            enum_yield_array(RARRAY_AREF(ary, i));
        }
    }
    return Qnil;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-detect" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">detect</span><span
                                class="method-args">(*args)</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the first element for which the block returns a truthy value.</p>

<p>With a block given, calls the block with successive elements of the collection; returns the first element for which the block returns a truthy value:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">find</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">2</span>}                <span class="ruby-comment"># =&gt; 3</span>
</pre>

<p>If no such element is found, calls <code>if_none_proc</code> and returns its return value.</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">find</span>(<span class="ruby-identifier">proc</span> {<span class="ruby-keyword">false</span>}) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">12</span>} <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">find</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;b&#39;</span>) }            <span class="ruby-comment"># =&gt; [:bar, 1]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">proc</span> {[]}) {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;c&#39;</span>) } <span class="ruby-comment"># =&gt; []</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-find">find</a>
                            </div>
                          </div>

                  <div id="method-i-drop" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          drop(n) &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>For positive integer <code>n</code>, returns an array containing all but the first <code>n</code> elements:</p>

<pre class="ruby"><span class="ruby-identifier">r</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>)
<span class="ruby-identifier">r</span>.<span class="ruby-identifier">drop</span>(<span class="ruby-value">3</span>)  <span class="ruby-comment"># =&gt; [4]</span>
<span class="ruby-identifier">r</span>.<span class="ruby-identifier">drop</span>(<span class="ruby-value">2</span>)  <span class="ruby-comment"># =&gt; [3, 4]</span>
<span class="ruby-identifier">r</span>.<span class="ruby-identifier">drop</span>(<span class="ruby-value">1</span>)  <span class="ruby-comment"># =&gt; [2, 3, 4]</span>
<span class="ruby-identifier">r</span>.<span class="ruby-identifier">drop</span>(<span class="ruby-value">0</span>)  <span class="ruby-comment"># =&gt; [1, 2, 3, 4]</span>
<span class="ruby-identifier">r</span>.<span class="ruby-identifier">drop</span>(<span class="ruby-value">50</span>) <span class="ruby-comment"># =&gt; []</span>

<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bat:</span> <span class="ruby-value">3</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">drop</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># =&gt; [[:baz, 2], [:bat, 3]]</span>
</pre>

                              <div class="method-source-code" id="drop-source">
            <pre>static VALUE
enum_drop(VALUE obj, VALUE n)
{
    VALUE result;
    struct MEMO *memo;
    long len = NUM2LONG(n);

    if (len &lt; 0) {
        rb_raise(rb_eArgError, &quot;attempt to drop negative size&quot;);
    }

    result = rb_ary_new();
    memo = MEMO_NEW(result, 0, len);
    rb_block_call(obj, id_each, 0, 0, drop_i, (VALUE)memo);
    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-drop_while" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          drop_while {|element| ... } &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          drop_while                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block with successive elements as long as the block returns a truthy value; returns an array of all elements after that point:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">drop_while</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">3</span> } <span class="ruby-comment"># =&gt; [3, 4]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">a</span> = <span class="ruby-identifier">h</span>.<span class="ruby-identifier">drop_while</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span> = <span class="ruby-operator">*</span><span class="ruby-identifier">element</span>; <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[:baz, 2]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="drop_while-source">
            <pre>static VALUE
enum_drop_while(VALUE obj)
{
    VALUE result;
    struct MEMO *memo;

    RETURN_ENUMERATOR(obj, 0, 0);
    result = rb_ary_new();
    memo = MEMO_NEW(result, 0, FALSE);
    rb_block_call(obj, id_each, 0, 0, drop_while_i, (VALUE)memo);
    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each_cons" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_cons(n) { ... } &rarr;  self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_cons(n)         &rarr;  enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block with each successive overlapped <code>n</code>-tuple of elements; returns <code>self</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = []
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">5</span>).<span class="ruby-identifier">each_cons</span>(<span class="ruby-value">3</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[1, 2, 3], [2, 3, 4], [3, 4, 5]]</span>

<span class="ruby-identifier">a</span> = []
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>,  <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bam:</span> <span class="ruby-value">3</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">each_cons</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[[:foo, 0], [:bar, 1]], [[:bar, 1], [:baz, 2]], [[:baz, 2], [:bam, 3]]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="each_cons-source">
            <pre>static VALUE
enum_each_cons(VALUE obj, VALUE n)
{
    long size = NUM2LONG(n);
    struct MEMO *memo;
    int arity;

    if (size &lt;= 0) rb_raise(rb_eArgError, &quot;invalid size&quot;);
    RETURN_SIZED_ENUMERATOR(obj, 1, &amp;n, enum_each_cons_size);
    arity = rb_block_arity();
    if (enum_size_over_p(obj, size)) return obj;
    memo = MEMO_NEW(rb_ary_new2(size), dont_recycle_block_arg(arity), size);
    rb_block_call(obj, id_each, 0, 0, each_cons_i, (VALUE)memo);

    return obj;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each_entry" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_entry(*args) {|element| ... } &rarr; self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_entry(*args)                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the given block with each element, converting multiple values from yield to an array; returns <code>self</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = []
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">each_entry</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) } <span class="ruby-comment"># =&gt; 1..4</span>
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [1, 2, 3, 4]</span>

<span class="ruby-identifier">a</span> = []
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span><span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">each_entry</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) }
<span class="ruby-comment"># =&gt; {:foo=&gt;0, :bar=&gt;1, :baz=&gt;2}</span>
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1], [:baz, 2]]</span>

<span class="ruby-keyword">class</span> <span class="ruby-constant">Foo</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">Enumerable</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">each</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>
    <span class="ruby-keyword">yield</span> <span class="ruby-value">1</span>, <span class="ruby-value">2</span>
    <span class="ruby-keyword">yield</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-constant">Foo</span>.<span class="ruby-identifier">new</span>.<span class="ruby-identifier">each_entry</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">yielded</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">yielded</span> }
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-value">1</span>
[<span class="ruby-value">1</span>, <span class="ruby-value">2</span>]
<span class="ruby-keyword">nil</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="each_entry-source">
            <pre>static VALUE
enum_each_entry(int argc, VALUE *argv, VALUE obj)
{
    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);
    rb_block_call(obj, id_each, argc, argv, each_val_i, 0);
    return obj;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each_slice" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_slice(n) { ... }  &rarr;  self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_slice(n)          &rarr;  enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block with each successive disjoint <code>n</code>-tuple of elements; returns <code>self</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = []
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">10</span>).<span class="ruby-identifier">each_slice</span>(<span class="ruby-value">3</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">tuple</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">tuple</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]</span>

<span class="ruby-identifier">a</span> = []
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bat:</span> <span class="ruby-value">3</span>, <span class="ruby-value">bam:</span> <span class="ruby-value">4</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">each_slice</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">tuple</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">tuple</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]], [[:bam, 4]]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="each_slice-source">
            <pre>static VALUE
enum_each_slice(VALUE obj, VALUE n)
{
    long size = NUM2LONG(n);
    VALUE ary;
    struct MEMO *memo;
    int arity;

    if (size &lt;= 0) rb_raise(rb_eArgError, &quot;invalid slice size&quot;);
    RETURN_SIZED_ENUMERATOR(obj, 1, &amp;n, enum_each_slice_size);
    size = limit_by_enum_size(obj, size);
    ary = rb_ary_new2(size);
    arity = rb_block_arity();
    memo = MEMO_NEW(ary, dont_recycle_block_arg(arity), size);
    rb_block_call(obj, id_each, 0, 0, each_slice_i, (VALUE)memo);
    ary = memo-&gt;v1;
    if (RARRAY_LEN(ary) &gt; 0) rb_yield(ary);

    return obj;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each_with_index" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_index(*args) {|element, i| ..... } &rarr; self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_index(*args)                       &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With a block given, calls the block with each element and its index; returns <code>self</code>:</p>

<pre class="ruby"><span class="ruby-identifier">h</span> = {}
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">each_with_index</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span>, <span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">h</span>[<span class="ruby-identifier">element</span>] = <span class="ruby-identifier">i</span> } <span class="ruby-comment"># =&gt; 1..4</span>
<span class="ruby-identifier">h</span> <span class="ruby-comment"># =&gt; {1=&gt;0, 2=&gt;1, 3=&gt;2, 4=&gt;3}</span>

<span class="ruby-identifier">h</span> = {}
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">each_with_index</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span>, <span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">h</span>[<span class="ruby-identifier">element</span>] = <span class="ruby-identifier">i</span> }
<span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]</span>
<span class="ruby-identifier">h</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;0, &quot;b&quot;=&gt;1, &quot;c&quot;=&gt;2, &quot;d&quot;=&gt;3}</span>

<span class="ruby-identifier">a</span> = []
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">each_with_index</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span>, <span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>([<span class="ruby-identifier">i</span>, <span class="ruby-identifier">element</span>]) }
<span class="ruby-comment"># =&gt; {:foo=&gt;0, :bar=&gt;1, :baz=&gt;2}</span>
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[0, [:foo, 0]], [1, [:bar, 1]], [2, [:baz, 2]]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="each_with_index-source">
            <pre>static VALUE
enum_each_with_index(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo;

    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);

    memo = MEMO_NEW(0, 0, 0);
    rb_block_call(obj, id_each, argc, argv, each_with_index_i, (VALUE)memo);
    return obj;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-each_with_object" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_object(object) { |(*args), memo_object| ... }  &rarr;  object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          each_with_object(object)                                 &rarr;  enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block once for each element, passing both the element and the given object:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">each_with_object</span>([]) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">a</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">i</span><span class="ruby-operator">**</span><span class="ruby-value">2</span>) }
<span class="ruby-comment"># =&gt; [1, 4, 9, 16]</span>

{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">each_with_object</span>({}) {<span class="ruby-operator">|</span>(<span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span>), <span class="ruby-identifier">h</span><span class="ruby-operator">|</span> <span class="ruby-identifier">h</span>[<span class="ruby-identifier">v</span>] = <span class="ruby-identifier">k</span> }
<span class="ruby-comment"># =&gt; {0=&gt;:foo, 1=&gt;:bar, 2=&gt;:baz}</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="each_with_object-source">
            <pre>static VALUE
enum_each_with_object(VALUE obj, VALUE memo)
{
    RETURN_SIZED_ENUMERATOR(obj, 1, &amp;memo, enum_size);

    rb_block_call(obj, id_each, 0, 0, each_with_object_i, memo);

    return memo;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-entries" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">entries</span><span
                                class="method-args">(*args)</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array containing the items in <code>self</code>:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">to_a</span> <span class="ruby-comment"># =&gt; [0, 1, 2, 3, 4]</span>
</pre>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-to_a">to_a</a>
                            </div>
                          </div>

                  <div id="method-i-filter" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">filter</span><span
                                class="method-args">()</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array containing elements selected by the block.</p>

<p>With a block given, calls the block with successive elements; returns an array of those elements for which the block returns a truthy value:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">select</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">%</span> <span class="ruby-value">3</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> } <span class="ruby-comment"># =&gt; [0, 3, 6, 9]</span>
<span class="ruby-identifier">a</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">select</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;b&#39;</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; {:bar=&gt;1, :baz=&gt;2}</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>Related: <a href="Enumerable.html#method-i-reject"><code>reject</code></a>.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-find_all">find_all</a>
                            </div>
                          </div>

                  <div id="method-i-filter_map" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          filter_map {|element| ... } &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          filter_map &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array containing truthy elements returned by the block.</p>

<p>With a block given, calls the block with successive elements; returns an array containing each truthy value returned by the block:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">filter_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">*</span> <span class="ruby-value">2</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">even?</span> }                              <span class="ruby-comment"># =&gt; [0, 4, 8, 12, 16]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">filter_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">even?</span> } <span class="ruby-comment"># =&gt; [:foo, :baz]</span>
</pre>

<p>When no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="filter_map-source">
            <pre>static VALUE
enum_filter_map(VALUE obj)
{
    VALUE ary;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    ary = rb_ary_new();
    rb_block_call(obj, id_each, 0, 0, filter_map_i, ary);

    return ary;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-find" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          find(if_none_proc = nil) {|element| ... } &rarr; object or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          find(if_none_proc = nil) &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the first element for which the block returns a truthy value.</p>

<p>With a block given, calls the block with successive elements of the collection; returns the first element for which the block returns a truthy value:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">find</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">2</span>}                <span class="ruby-comment"># =&gt; 3</span>
</pre>

<p>If no such element is found, calls <code>if_none_proc</code> and returns its return value.</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">find</span>(<span class="ruby-identifier">proc</span> {<span class="ruby-keyword">false</span>}) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">12</span>} <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">find</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;b&#39;</span>) }            <span class="ruby-comment"># =&gt; [:bar, 1]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">find</span>(<span class="ruby-identifier">proc</span> {[]}) {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;c&#39;</span>) } <span class="ruby-comment"># =&gt; []</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="find-source">
            <pre>static VALUE
enum_find(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo;
    VALUE if_none;

    if_none = rb_check_arity(argc, 0, 1) ? argv[0] : Qnil;
    RETURN_ENUMERATOR(obj, argc, argv);
    memo = MEMO_NEW(Qundef, 0, 0);
    rb_block_call(obj, id_each, 0, 0, find_i, (VALUE)memo);
    if (memo-&gt;u3.cnt) {
        return memo-&gt;v1;
    }
    if (!NIL_P(if_none)) {
        return rb_funcallv(if_none, id_call, 0, 0);
    }
    return Qnil;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerable.html#method-i-detect">detect</a>
                            </div>

                          </div>

                  <div id="method-i-find_all" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">find_all</span><span
                                class="method-args"> -&gt; enumerator</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array containing elements selected by the block.</p>

<p>With a block given, calls the block with successive elements; returns an array of those elements for which the block returns a truthy value:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">select</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">%</span> <span class="ruby-value">3</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> } <span class="ruby-comment"># =&gt; [0, 3, 6, 9]</span>
<span class="ruby-identifier">a</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">select</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;b&#39;</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; {:bar=&gt;1, :baz=&gt;2}</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>Related: <a href="Enumerable.html#method-i-reject"><code>reject</code></a>.</p>

                              <div class="method-source-code" id="find_all-source">
            <pre>static VALUE
enum_find_all(VALUE obj)
{
    VALUE ary;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    ary = rb_ary_new();
    rb_block_call(obj, id_each, 0, 0, find_all_i, ary);

    return ary;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerable.html#method-i-select">select</a>, <a href="Enumerable.html#method-i-filter">filter</a>
                            </div>

                          </div>

                  <div id="method-i-find_index" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          find_index(object) &rarr; integer or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          find_index {|element| ... } &rarr; integer or nil
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          find_index &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the index of the first element that meets a specified criterion, or <code>nil</code> if no such element is found.</p>

<p>With argument <code>object</code> given, returns the index of the first element that is <code>==</code> <code>object</code>:</p>

<pre class="ruby">[<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-string">&#39;b&#39;</span>, <span class="ruby-string">&#39;c&#39;</span>, <span class="ruby-string">&#39;b&#39;</span>].<span class="ruby-identifier">find_index</span>(<span class="ruby-string">&#39;b&#39;</span>) <span class="ruby-comment"># =&gt; 1</span>
</pre>

<p>With a block given, calls the block with successive elements; returns the first element for which the block returns a truthy value:</p>

<pre class="ruby">[<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-string">&#39;b&#39;</span>, <span class="ruby-string">&#39;c&#39;</span>, <span class="ruby-string">&#39;b&#39;</span>].<span class="ruby-identifier">find_index</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;b&#39;</span>) } <span class="ruby-comment"># =&gt; 1</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">find_index</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span> }         <span class="ruby-comment"># =&gt; 2</span>
</pre>

<p>With no argument and no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="find_index-source">
            <pre>static VALUE
enum_find_index(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo;  /* [return value, current index, ] */
    VALUE condition_value = Qnil;
    rb_block_call_func *func;

    if (argc == 0) {
        RETURN_ENUMERATOR(obj, 0, 0);
        func = find_index_iter_i;
    }
    else {
        rb_scan_args(argc, argv, &quot;1&quot;, &amp;condition_value);
        if (rb_block_given_p()) {
            rb_warn(&quot;given block not used&quot;);
        }
        func = find_index_i;
    }

    memo = MEMO_NEW(Qnil, condition_value, 0);
    rb_block_call(obj, id_each, 0, 0, func, (VALUE)memo);
    return memo-&gt;v1;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-first" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          first    &rarr; element or nil
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          first(n) &rarr; array
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the first element or elements.</p>

<p>With no argument, returns the first element, or <code>nil</code> if there is none:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">first</span>                   <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-node">%w[a b c]</span>.<span class="ruby-identifier">first</span>                <span class="ruby-comment"># =&gt; &quot;a&quot;</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">1</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">first</span> <span class="ruby-comment"># =&gt; [:foo, 1]</span>
[].<span class="ruby-identifier">first</span>                       <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With integer argument <code>n</code>, returns an array containing the first <code>n</code> elements that exist:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">first</span>(<span class="ruby-value">2</span>)                   <span class="ruby-comment"># =&gt; [1, 2]</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">first</span>(<span class="ruby-value">3</span>)              <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">first</span>(<span class="ruby-value">50</span>)             <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">1</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">first</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># =&gt; [[:foo, 1], [:bar, 1]]</span>
[].<span class="ruby-identifier">first</span>(<span class="ruby-value">2</span>)                       <span class="ruby-comment"># =&gt; []</span>
</pre>

                              <div class="method-source-code" id="first-source">
            <pre>static VALUE
enum_first(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo;
    rb_check_arity(argc, 0, 1);
    if (argc &gt; 0) {
        return enum_take(obj, argv[0]);
    }
    else {
        memo = MEMO_NEW(Qnil, 0, 0);
        rb_block_call(obj, id_each, 0, 0, first_i, (VALUE)memo);
        return memo-&gt;v1;
    }
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-flat_map" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          flat_map {|element| ... } &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          flat_map &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of flattened objects returned by the block.</p>

<p>With a block given, calls the block with successive elements; returns a flattened array of objects returned by the block:</p>

<pre class="ruby">[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }                    <span class="ruby-comment"># =&gt; [0, -1, -2, -3]</span>
[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>].<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">element</span>, <span class="ruby-operator">-</span><span class="ruby-identifier">element</span>] }         <span class="ruby-comment"># =&gt; [0, 0, 1, -1, 2, -2, 3, -3]</span>
[[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>], [<span class="ruby-value">2</span>, <span class="ruby-value">3</span>]].<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">e</span> <span class="ruby-operator">+</span> [<span class="ruby-value">100</span>] }                     <span class="ruby-comment"># =&gt; [0, 1, 100, 2, 3, 100]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span>] } <span class="ruby-comment"># =&gt; [:foo, 0, :bar, 1, :baz, 2]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>Alias: <a href="Enumerable.html#method-i-collect_concat"><code>collect_concat</code></a>.</p>

                              <div class="method-source-code" id="flat_map-source">
            <pre>static VALUE
enum_flat_map(VALUE obj)
{
    VALUE ary;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    ary = rb_ary_new();
    rb_block_call(obj, id_each, 0, 0, flat_map_i, ary);

    return ary;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerable.html#method-i-collect_concat">collect_concat</a>
                            </div>

                          </div>

                  <div id="method-i-grep" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          grep(pattern) &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          grep(pattern) {|element| ... } &rarr; array
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of objects based elements of <code>self</code> that match the given pattern.</p>

<p>With no block given, returns an array containing each element for which <code>pattern === element</code> is <code>true</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-string">&#39;car&#39;</span>, <span class="ruby-string">&#39;moo&#39;</span>]
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">grep</span>(<span class="ruby-regexp">/ar/</span>)                   <span class="ruby-comment"># =&gt; [&quot;bar&quot;, &quot;car&quot;]</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">10</span>).<span class="ruby-identifier">grep</span>(<span class="ruby-value">3</span><span class="ruby-operator">..</span><span class="ruby-value">8</span>)             <span class="ruby-comment"># =&gt; [3, 4, 5, 6, 7, 8]</span>
[<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-string">&#39;b&#39;</span>, <span class="ruby-value">0</span>, <span class="ruby-value">1</span>].<span class="ruby-identifier">grep</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-comment"># =&gt; [0, 1]</span>
</pre>

<p>With a block given, calls the block with each matching element and returns an array containing each object returned by the block:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-string">&#39;car&#39;</span>, <span class="ruby-string">&#39;moo&#39;</span>]
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">grep</span>(<span class="ruby-regexp">/ar/</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">upcase</span> } <span class="ruby-comment"># =&gt; [&quot;BAR&quot;, &quot;CAR&quot;]</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-grep_v"><code>grep_v</code></a>.</p>

                              <div class="method-source-code" id="grep-source">
            <pre>static VALUE
enum_grep(VALUE obj, VALUE pat)
{
    return enum_grep0(obj, pat, Qtrue);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-grep_v" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          grep_v(pattern) &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          grep_v(pattern) {|element| ... } &rarr; array
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of objects based on elements of <code>self</code> that <em>don’t</em> match the given pattern.</p>

<p>With no block given, returns an array containing each element for which <code>pattern === element</code> is <code>false</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-string">&#39;car&#39;</span>, <span class="ruby-string">&#39;moo&#39;</span>]
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">grep_v</span>(<span class="ruby-regexp">/ar/</span>)                   <span class="ruby-comment"># =&gt; [&quot;foo&quot;, &quot;moo&quot;]</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">10</span>).<span class="ruby-identifier">grep_v</span>(<span class="ruby-value">3</span><span class="ruby-operator">..</span><span class="ruby-value">8</span>)             <span class="ruby-comment"># =&gt; [1, 2, 9, 10]</span>
[<span class="ruby-string">&#39;a&#39;</span>, <span class="ruby-string">&#39;b&#39;</span>, <span class="ruby-value">0</span>, <span class="ruby-value">1</span>].<span class="ruby-identifier">grep_v</span>(<span class="ruby-constant">Integer</span>) <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;]</span>
</pre>

<p>With a block given, calls the block with each non-matching element and returns an array containing each object returned by the block:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-string">&#39;bar&#39;</span>, <span class="ruby-string">&#39;car&#39;</span>, <span class="ruby-string">&#39;moo&#39;</span>]
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">grep_v</span>(<span class="ruby-regexp">/ar/</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span>.<span class="ruby-identifier">upcase</span> } <span class="ruby-comment"># =&gt; [&quot;FOO&quot;, &quot;MOO&quot;]</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-grep"><code>grep</code></a>.</p>

                              <div class="method-source-code" id="grep_v-source">
            <pre>static VALUE
enum_grep_v(VALUE obj, VALUE pat)
{
    return enum_grep0(obj, pat, Qfalse);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-group_by" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          group_by {|element| ... } &rarr; hash
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          group_by                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With a block given returns a hash:</p>
<ul><li>
<p>Each key is a return value from the block.</p>
</li><li>
<p>Each value is an array of those elements for which the block returned that key.</p>
</li></ul>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-identifier">g</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">6</span>).<span class="ruby-identifier">group_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span><span class="ruby-operator">%</span><span class="ruby-value">3</span> }
<span class="ruby-identifier">g</span> <span class="ruby-comment"># =&gt; {1=&gt;[1, 4], 2=&gt;[2, 5], 0=&gt;[3, 6]}</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bat:</span> <span class="ruby-value">1</span>}
<span class="ruby-identifier">g</span> = <span class="ruby-identifier">h</span>.<span class="ruby-identifier">group_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> }
<span class="ruby-identifier">g</span> <span class="ruby-comment"># =&gt; {0=&gt;[[:foo, 0], [:baz, 0]], 1=&gt;[[:bar, 1], [:bat, 1]]}</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="group_by-source">
            <pre>static VALUE
enum_group_by(VALUE obj)
{
    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    return enum_hashify(obj, 0, 0, group_by_i);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-include-3F" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          include?(object) &rarr; true or false
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns whether for any element <code>object == element</code>:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">include?</span>(<span class="ruby-value">2</span>)                       <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">include?</span>(<span class="ruby-value">5</span>)                       <span class="ruby-comment"># =&gt; false</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;2&#39;</span>)                     <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;b&#39;</span>)                <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;2&#39;</span>)                <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">include?</span>(<span class="ruby-value">:foo</span>)  <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">include?</span>(<span class="ruby-value">0</span>)     <span class="ruby-comment"># =&gt; false</span>
</pre>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-member-3F">member?</a>
                            </div>
                          </div>

                  <div id="method-i-inject" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          inject(symbol) &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          inject(initial_operand, symbol) &rarr; object
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          inject {|memo, operand| ... } &rarr; object
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          inject(initial_operand) {|memo, operand| ... } &rarr; object
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an object formed from operands via either:</p>
<ul><li>
<p>A method named by <code>symbol</code>.</p>
</li><li>
<p>A block to which each operand is passed.</p>
</li></ul>

<p>With method-name argument <code>symbol</code>, combines operands using the method:</p>

<pre class="ruby"><span class="ruby-comment"># Sum, without initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)     <span class="ruby-comment"># =&gt; 10</span>
<span class="ruby-comment"># Sum, with initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">10</span>, <span class="ruby-value">:+</span>) <span class="ruby-comment"># =&gt; 20</span>
</pre>

<p>With a block, passes each operand to the block:</p>

<pre class="ruby"><span class="ruby-comment"># Sum of squares, without initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">sum</span>, <span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">sum</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">n</span><span class="ruby-operator">*</span><span class="ruby-identifier">n</span> }    <span class="ruby-comment"># =&gt; 30</span>
<span class="ruby-comment"># Sum of squares, with initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sum</span>, <span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">sum</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">n</span><span class="ruby-operator">*</span><span class="ruby-identifier">n</span> } <span class="ruby-comment"># =&gt; 32</span>
</pre>

<p><strong>Operands</strong></p>

<p>If argument <code>initial_operand</code> is not given, the operands for <code>inject</code> are simply the elements of <code>self</code>. Example calls and their operands:</p>
<ul><li><dl class="rdoc-list note-list"><dt><code>(1..4).inject(:+)</code>
<dd>
<p><code>[1, 2, 3, 4]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(1...4).inject(:+)</code>
<dd>
<p><code>[1, 2, 3]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;..&#39;d&#39;).inject(:+)</code>
<dd>
<p><code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;...&#39;d&#39;).inject(:+)</code>
<dd>
<p><code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</code>.</p>
</dd></dl>
</li></ul>

<p>Examples with first operand (which is <code>self.first</code>) of various types:</p>

<pre class="ruby"><span class="ruby-comment"># Integer.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)                <span class="ruby-comment"># =&gt; 10</span>
<span class="ruby-comment"># Float.</span>
[<span class="ruby-value">1.0</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">4</span>].<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)        <span class="ruby-comment"># =&gt; 10.0</span>
<span class="ruby-comment"># Character.</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)            <span class="ruby-comment"># =&gt; &quot;abcd&quot;</span>
<span class="ruby-comment"># Complex.</span>
[<span class="ruby-constant">Complex</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>), <span class="ruby-value">3</span>, <span class="ruby-value">4</span>].<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>) <span class="ruby-comment"># =&gt; (8+2i)</span>
</pre>

<p>If argument <code>initial_operand</code> is given, the operands for <code>inject</code> are that value plus the elements of <code>self</code>. Example calls their operands:</p>
<ul><li><dl class="rdoc-list note-list"><dt><code>(1..4).inject(10, :+)</code>
<dd>
<p><code>[10, 1, 2, 3, 4]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(1...4).inject(10, :+)</code>
<dd>
<p><code>[10, 1, 2, 3]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;..&#39;d&#39;).inject(&#39;e&#39;, :+)</code>
<dd>
<p><code>[&#39;e&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;...&#39;d&#39;).inject(&#39;e&#39;, :+)</code>
<dd>
<p><code>[&#39;e&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</code>.</p>
</dd></dl>
</li></ul>

<p>Examples with <code>initial_operand</code> of various types:</p>

<pre class="ruby"><span class="ruby-comment"># Integer.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">2</span>, <span class="ruby-value">:+</span>)               <span class="ruby-comment"># =&gt; 12</span>
<span class="ruby-comment"># Float.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">2.0</span>, <span class="ruby-value">:+</span>)             <span class="ruby-comment"># =&gt; 12.0</span>
<span class="ruby-comment"># String.</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">:+</span>)       <span class="ruby-comment"># =&gt; &quot;fooabcd&quot;</span>
<span class="ruby-comment"># Array.</span>
<span class="ruby-node">%w[a b c]</span>.<span class="ruby-identifier">inject</span>([<span class="ruby-string">&#39;x&#39;</span>], <span class="ruby-value">:push</span>)     <span class="ruby-comment"># =&gt; [&quot;x&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
<span class="ruby-comment"># Complex.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-constant">Complex</span>(<span class="ruby-value">2</span>, <span class="ruby-value">2</span>), <span class="ruby-value">:+</span>)   <span class="ruby-comment"># =&gt; (12+2i)</span>
</pre>

<p><strong>Combination by Given Method</strong></p>

<p>If the method-name argument <code>symbol</code> is given, the operands are combined by that method:</p>
<ul><li>
<p>The first and second operands are combined.</p>
</li><li>
<p>That result is combined with the third operand.</p>
</li><li>
<p>That result is combined with the fourth operand.</p>
</li><li>
<p>And so on.</p>
</li></ul>

<p>The return value from <code>inject</code> is the result of the last combination.</p>

<p>This call to <code>inject</code> computes the sum of the operands:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>) <span class="ruby-comment"># =&gt; 10</span>
</pre>

<p>Examples with various methods:</p>

<pre class="ruby"><span class="ruby-comment"># Integer addition.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)                <span class="ruby-comment"># =&gt; 10</span>
<span class="ruby-comment"># Integer multiplication.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:*</span>)                <span class="ruby-comment"># =&gt; 24</span>
<span class="ruby-comment"># Character range concatenation.</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">:+</span>)        <span class="ruby-comment"># =&gt; &quot;abcd&quot;</span>
<span class="ruby-comment"># String array concatenation.</span>
<span class="ruby-node">%w[foo bar baz]</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">:+</span>)   <span class="ruby-comment"># =&gt; &quot;foobarbaz&quot;</span>
<span class="ruby-comment"># Hash update.</span>
<span class="ruby-identifier">h</span> = [{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>}, {<span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}, {<span class="ruby-value">bat:</span> <span class="ruby-value">3</span>}].<span class="ruby-identifier">inject</span>(<span class="ruby-value">:update</span>)
<span class="ruby-identifier">h</span> <span class="ruby-comment"># =&gt; {:foo=&gt;0, :bar=&gt;1, :baz=&gt;2, :bat=&gt;3}</span>
<span class="ruby-comment"># Hash conversion to nested arrays.</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>}.<span class="ruby-identifier">inject</span>([], <span class="ruby-value">:push</span>)
<span class="ruby-identifier">h</span> <span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1]]</span>
</pre>

<p><strong>Combination by Given Block</strong></p>

<p>If a block is given, the operands are passed to the block:</p>
<ul><li>
<p>The first call passes the first and second operands.</p>
</li><li>
<p>The second call passes the result of the first call, along with the third operand.</p>
</li><li>
<p>The third call passes the result of the second call, along with the fourth operand.</p>
</li><li>
<p>And so on.</p>
</li></ul>

<p>The return value from <code>inject</code> is the return value from the last block call.</p>

<p>This call to <code>inject</code> gives a block that writes the memo and element, and also sums the elements:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">memo</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-node">&quot;Memo: #{memo}; element: #{element}&quot;</span>
  <span class="ruby-identifier">memo</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">element</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; 10</span>
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-string">&quot;Memo: 1; element: 2&quot;</span>
<span class="ruby-string">&quot;Memo: 3; element: 3&quot;</span>
<span class="ruby-string">&quot;Memo: 6; element: 4&quot;</span>
</pre>

                              <div class="method-source-code" id="inject-source">
            <pre>static VALUE
enum_inject(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo;
    VALUE init, op;
    rb_block_call_func *iter = inject_i;
    ID id;
    int num_args;

    if (rb_block_given_p()) {
        num_args = rb_scan_args(argc, argv, &quot;02&quot;, &amp;init, &amp;op);
    }
    else {
        num_args = rb_scan_args(argc, argv, &quot;11&quot;, &amp;init, &amp;op);
    }

    switch (num_args) {
      case 0:
        init = Qundef;
        break;
      case 1:
        if (rb_block_given_p()) {
            break;
        }
        id = rb_check_id(&amp;init);
        op = id ? ID2SYM(id) : init;
        init = Qundef;
        iter = inject_op_i;
        break;
      case 2:
        if (rb_block_given_p()) {
            rb_warning(&quot;given block not used&quot;);
        }
        id = rb_check_id(&amp;op);
        if (id) op = ID2SYM(id);
        iter = inject_op_i;
        break;
    }

    if (iter == inject_op_i &amp;&amp;
        SYMBOL_P(op) &amp;&amp;
        RB_TYPE_P(obj, T_ARRAY) &amp;&amp;
        rb_method_basic_definition_p(CLASS_OF(obj), id_each)) {
        return ary_inject_op(obj, init, op);
    }

    memo = MEMO_NEW(init, Qnil, op);
    rb_block_call(obj, id_each, 0, 0, iter, (VALUE)memo);
    if (UNDEF_P(memo-&gt;v1)) return Qnil;
    return memo-&gt;v1;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerable.html#method-i-reduce">reduce</a>
                            </div>

                          </div>

                  <div id="method-i-lazy" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          lazy &rarr; lazy_enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an <a href="Enumerator/Lazy.html"><code>Enumerator::Lazy</code></a>, which redefines most <a href="Enumerable.html"><code>Enumerable</code></a> methods to postpone enumeration and enumerate values only on an as-needed basis.</p>

<h3 id="method-i-lazy-label-Example">Example<span><a href="#method-i-lazy-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The following program finds pythagorean triples:</p>

<pre class="ruby"><span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pythagorean_triples</span>
  (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-constant">Float</span><span class="ruby-operator">::</span><span class="ruby-constant">INFINITY</span>).<span class="ruby-identifier">lazy</span>.<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">z</span><span class="ruby-operator">|</span>
    (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-identifier">z</span>).<span class="ruby-identifier">flat_map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">x</span><span class="ruby-operator">|</span>
      (<span class="ruby-identifier">x</span><span class="ruby-operator">..</span><span class="ruby-identifier">z</span>).<span class="ruby-identifier">select</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">x</span><span class="ruby-operator">**</span><span class="ruby-value">2</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">y</span><span class="ruby-operator">**</span><span class="ruby-value">2</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">z</span><span class="ruby-operator">**</span><span class="ruby-value">2</span>
      }.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
        [<span class="ruby-identifier">x</span>, <span class="ruby-identifier">y</span>, <span class="ruby-identifier">z</span>]
      }
    }
  }
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># show first ten pythagorean triples</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">pythagorean_triples</span>.<span class="ruby-identifier">take</span>(<span class="ruby-value">10</span>).<span class="ruby-identifier">force</span> <span class="ruby-comment"># take is lazy, so force is needed</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">pythagorean_triples</span>.<span class="ruby-identifier">first</span>(<span class="ruby-value">10</span>)      <span class="ruby-comment"># first is eager</span>
<span class="ruby-comment"># show pythagorean triples less than 100</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">pythagorean_triples</span>.<span class="ruby-identifier">take_while</span> { <span class="ruby-operator">|</span><span class="ruby-operator">*</span>, <span class="ruby-identifier">z</span><span class="ruby-operator">|</span> <span class="ruby-identifier">z</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">100</span> }.<span class="ruby-identifier">force</span>
</pre>

                              <div class="method-source-code" id="lazy-source">
            <pre>static VALUE
enumerable_lazy(VALUE obj)
{
    VALUE result = lazy_to_enum_i(obj, sym_each, 0, 0, lazyenum_size, rb_keyword_given_p());
    /* Qfalse indicates that the Enumerator::Lazy has no method name */
    rb_ivar_set(result, id_method, Qfalse);
    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-map" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          map {|element| ... } &rarr; array
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          map &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of objects returned by the block.</p>

<p>With a block given, calls the block with successive elements; returns an array of the objects returned by the block:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span><span class="ruby-operator">*</span><span class="ruby-identifier">i</span> }                               <span class="ruby-comment"># =&gt; [0, 1, 4, 9, 16]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span><span class="ruby-operator">*</span><span class="ruby-value">2</span>} <span class="ruby-comment"># =&gt; [0, 2, 4]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-collect">collect</a>
                            </div>
                          </div>

                  <div id="method-i-max" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          max                  &rarr; element
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          max(n)               &rarr; array
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          max {|a, b| ... }    &rarr; element
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          max(n) {|a, b| ... } &rarr; array
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the element with the maximum element according to a given criterion. The ordering of equal elements is indeterminate and may be unstable.</p>

<p>With no argument and no block, returns the maximum element, using the elements’ own method <code>&lt;=&gt;</code> for comparison:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">max</span>                   <span class="ruby-comment"># =&gt; 4</span>
(<span class="ruby-value">-4</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>).<span class="ruby-identifier">max</span>                 <span class="ruby-comment"># =&gt; -1</span>
<span class="ruby-node">%w[d c b a]</span>.<span class="ruby-identifier">max</span>              <span class="ruby-comment"># =&gt; &quot;d&quot;</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">max</span> <span class="ruby-comment"># =&gt; [:foo, 0]</span>
[].<span class="ruby-identifier">max</span>                       <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With positive integer argument <code>n</code> given, and no block, returns an array containing the first <code>n</code> maximum elements that exist:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>)                   <span class="ruby-comment"># =&gt; [4, 3]</span>
(<span class="ruby-value">-4</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>).<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>)                <span class="ruby-comment"># =&gt; [-1, -2]</span>
<span class="ruby-node">%w[d c b a]</span>.<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>)              <span class="ruby-comment"># =&gt; [&quot;d&quot;, &quot;c&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># =&gt; [[:foo, 0], [:baz, 2]]</span>
[].<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>)                       <span class="ruby-comment"># =&gt; []</span>
</pre>

<p>With a block given, the block determines the maximum elements. The block is called with two elements <code>a</code> and <code>b</code>, and must return:</p>
<ul><li>
<p>A negative integer if <code>a &lt; b</code>.</p>
</li><li>
<p>Zero if <code>a == b</code>.</p>
</li><li>
<p>A positive integer if <code>a &gt; b</code>.</p>
</li></ul>

<p>With a block given and no argument, returns the maximum element as determined by the block:</p>

<pre class="ruby"><span class="ruby-node">%w[xxx x xxxx xx]</span>.<span class="ruby-identifier">max</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">size</span> } <span class="ruby-comment"># =&gt; &quot;xxxx&quot;</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">max</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">pair1</span>, <span class="ruby-identifier">pair2</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pair1</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">pair2</span>[<span class="ruby-value">1</span>] }     <span class="ruby-comment"># =&gt; [:baz, 2]</span>
[].<span class="ruby-identifier">max</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span> }                          <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With a block given and positive integer argument <code>n</code> given, returns an array containing the first <code>n</code> maximum elements that exist, as determined by the block.</p>

<pre class="ruby"><span class="ruby-node">%w[xxx x xxxx xx]</span>.<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">size</span> } <span class="ruby-comment"># =&gt; [&quot;xxxx&quot;, &quot;xxx&quot;]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">pair1</span>, <span class="ruby-identifier">pair2</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pair1</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">pair2</span>[<span class="ruby-value">1</span>] }
<span class="ruby-comment"># =&gt; [[:baz, 2], [:bar, 1]]</span>
[].<span class="ruby-identifier">max</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span> }                          <span class="ruby-comment"># =&gt; []</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-min"><code>min</code></a>, <a href="Enumerable.html#method-i-minmax"><code>minmax</code></a>, <a href="Enumerable.html#method-i-max_by"><code>max_by</code></a>.</p>

                              <div class="method-source-code" id="max-source">
            <pre>static VALUE
enum_max(int argc, VALUE *argv, VALUE obj)
{
    VALUE memo;
    struct max_t *m = NEW_MEMO_FOR(struct max_t, memo);
    VALUE result;
    VALUE num;

    if (rb_check_arity(argc, 0, 1) &amp;&amp; !NIL_P(num = argv[0]))
       return rb_nmin_run(obj, num, 0, 1, 0);

    m-&gt;max = Qundef;
    if (rb_block_given_p()) {
        rb_block_call(obj, id_each, 0, 0, max_ii, (VALUE)memo);
    }
    else {
        rb_block_call(obj, id_each, 0, 0, max_i, (VALUE)memo);
    }
    result = m-&gt;max;
    if (UNDEF_P(result)) return Qnil;
    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-max_by" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          max_by {|element| ... }    &rarr; element
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          max_by(n) {|element| ... } &rarr; array
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          max_by                     &rarr; enumerator
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          max_by(n)                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the elements for which the block returns the maximum values.</p>

<p>With a block given and no argument, returns the element for which the block returns the maximum value:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">max_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }                    <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">max_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span>.<span class="ruby-identifier">ord</span> }           <span class="ruby-comment"># =&gt; &quot;a&quot;</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">max_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">value</span> } <span class="ruby-comment"># =&gt; [:foo, 0]</span>
[].<span class="ruby-identifier">max_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }                        <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With a block given and positive integer argument <code>n</code> given, returns an array containing the <code>n</code> elements for which the block returns maximum values:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">max_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }
<span class="ruby-comment"># =&gt; [1, 2]</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">max_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span>.<span class="ruby-identifier">ord</span> }
<span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">max_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">value</span> }
<span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1]]</span>
[].<span class="ruby-identifier">max_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }
<span class="ruby-comment"># =&gt; []</span>
</pre>

<p>Returns an <a href="Enumerator.html"><code>Enumerator</code></a> if no block is given.</p>

<p>Related: <a href="Enumerable.html#method-i-max"><code>max</code></a>, <a href="Enumerable.html#method-i-minmax"><code>minmax</code></a>, <a href="Enumerable.html#method-i-min_by"><code>min_by</code></a>.</p>

                              <div class="method-source-code" id="max_by-source">
            <pre>static VALUE
enum_max_by(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo;
    VALUE num;

    rb_check_arity(argc, 0, 1);

    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);

    if (argc &amp;&amp; !NIL_P(num = argv[0]))
        return rb_nmin_run(obj, num, 1, 1, 0);

    memo = MEMO_NEW(Qundef, Qnil, 0);
    rb_block_call(obj, id_each, 0, 0, max_by_i, (VALUE)memo);
    return memo-&gt;v2;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-member-3F" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">member?</span><span
                                class="method-args">(object) -&gt; true or false</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns whether for any element <code>object == element</code>:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">include?</span>(<span class="ruby-value">2</span>)                       <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">include?</span>(<span class="ruby-value">5</span>)                       <span class="ruby-comment"># =&gt; false</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;2&#39;</span>)                     <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;b&#39;</span>)                <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;2&#39;</span>)                <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">include?</span>(<span class="ruby-value">:foo</span>)  <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">include?</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">include?</span>(<span class="ruby-value">0</span>)     <span class="ruby-comment"># =&gt; false</span>
</pre>

                              <div class="method-source-code" id="member-3F-source">
            <pre>static VALUE
enum_member(VALUE obj, VALUE val)
{
    struct MEMO *memo = MEMO_NEW(val, Qfalse, 0);

    rb_block_call(obj, id_each, 0, 0, member_i, (VALUE)memo);
    return memo-&gt;v2;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerable.html#method-i-include-3F">include?</a>
                            </div>

                          </div>

                  <div id="method-i-min" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          min                  &rarr; element
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          min(n)               &rarr; array
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          min {|a, b| ... }    &rarr; element
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          min(n) {|a, b| ... } &rarr; array
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the element with the minimum element according to a given criterion. The ordering of equal elements is indeterminate and may be unstable.</p>

<p>With no argument and no block, returns the minimum element, using the elements’ own method <code>&lt;=&gt;</code> for comparison:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">min</span>                   <span class="ruby-comment"># =&gt; 1</span>
(<span class="ruby-value">-4</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>).<span class="ruby-identifier">min</span>                 <span class="ruby-comment"># =&gt; -4</span>
<span class="ruby-node">%w[d c b a]</span>.<span class="ruby-identifier">min</span>              <span class="ruby-comment"># =&gt; &quot;a&quot;</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">min</span> <span class="ruby-comment"># =&gt; [:bar, 1]</span>
[].<span class="ruby-identifier">min</span>                       <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With positive integer argument <code>n</code> given, and no block, returns an array containing the first <code>n</code> minimum elements that exist:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>)                   <span class="ruby-comment"># =&gt; [1, 2]</span>
(<span class="ruby-value">-4</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>).<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>)                 <span class="ruby-comment"># =&gt; [-4, -3]</span>
<span class="ruby-node">%w[d c b a]</span>.<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>)              <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># =&gt; [[:bar, 1], [:baz, 2]]</span>
[].<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>)                       <span class="ruby-comment"># =&gt; []</span>
</pre>

<p>With a block given, the block determines the minimum elements. The block is called with two elements <code>a</code> and <code>b</code>, and must return:</p>
<ul><li>
<p>A negative integer if <code>a &lt; b</code>.</p>
</li><li>
<p>Zero if <code>a == b</code>.</p>
</li><li>
<p>A positive integer if <code>a &gt; b</code>.</p>
</li></ul>

<p>With a block given and no argument, returns the minimum element as determined by the block:</p>

<pre class="ruby"><span class="ruby-node">%w[xxx x xxxx xx]</span>.<span class="ruby-identifier">min</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">size</span> } <span class="ruby-comment"># =&gt; &quot;x&quot;</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">min</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">pair1</span>, <span class="ruby-identifier">pair2</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pair1</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">pair2</span>[<span class="ruby-value">1</span>] } <span class="ruby-comment"># =&gt; [:foo, 0]</span>
[].<span class="ruby-identifier">min</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span> }                          <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With a block given and positive integer argument <code>n</code> given, returns an array containing the first <code>n</code> minimum elements that exist, as determined by the block.</p>

<pre class="ruby"><span class="ruby-node">%w[xxx x xxxx xx]</span>.<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">size</span> } <span class="ruby-comment"># =&gt; [&quot;x&quot;, &quot;xx&quot;]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">pair1</span>, <span class="ruby-identifier">pair2</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pair1</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">pair2</span>[<span class="ruby-value">1</span>] }
<span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1]]</span>
[].<span class="ruby-identifier">min</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span> }                          <span class="ruby-comment"># =&gt; []</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-min_by"><code>min_by</code></a>, <a href="Enumerable.html#method-i-minmax"><code>minmax</code></a>, <a href="Enumerable.html#method-i-max"><code>max</code></a>.</p>

                              <div class="method-source-code" id="min-source">
            <pre>static VALUE
enum_min(int argc, VALUE *argv, VALUE obj)
{
    VALUE memo;
    struct min_t *m = NEW_MEMO_FOR(struct min_t, memo);
    VALUE result;
    VALUE num;

    if (rb_check_arity(argc, 0, 1) &amp;&amp; !NIL_P(num = argv[0]))
       return rb_nmin_run(obj, num, 0, 0, 0);

    m-&gt;min = Qundef;
    if (rb_block_given_p()) {
        rb_block_call(obj, id_each, 0, 0, min_ii, memo);
    }
    else {
        rb_block_call(obj, id_each, 0, 0, min_i, memo);
    }
    result = m-&gt;min;
    if (UNDEF_P(result)) return Qnil;
    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-min_by" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          min_by {|element| ... }    &rarr; element
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          min_by(n) {|element| ... } &rarr; array
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          min_by                     &rarr; enumerator
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          min_by(n)                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns the elements for which the block returns the minimum values.</p>

<p>With a block given and no argument, returns the element for which the block returns the minimum value:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">min_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }                    <span class="ruby-comment"># =&gt; 4</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">min_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span>.<span class="ruby-identifier">ord</span> }           <span class="ruby-comment"># =&gt; &quot;d&quot;</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">min_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">value</span> } <span class="ruby-comment"># =&gt; [:baz, 2]</span>
[].<span class="ruby-identifier">min_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }                        <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>With a block given and positive integer argument <code>n</code> given, returns an array containing the <code>n</code> elements for which the block returns minimum values:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">min_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }
<span class="ruby-comment"># =&gt; [4, 3]</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">min_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span>.<span class="ruby-identifier">ord</span> }
<span class="ruby-comment"># =&gt; [&quot;d&quot;, &quot;c&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">min_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">value</span> }
<span class="ruby-comment"># =&gt; [[:baz, 2], [:bar, 1]]</span>
[].<span class="ruby-identifier">min_by</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }
<span class="ruby-comment"># =&gt; []</span>
</pre>

<p>Returns an <a href="Enumerator.html"><code>Enumerator</code></a> if no block is given.</p>

<p>Related: <a href="Enumerable.html#method-i-min"><code>min</code></a>, <a href="Enumerable.html#method-i-minmax"><code>minmax</code></a>, <a href="Enumerable.html#method-i-max_by"><code>max_by</code></a>.</p>

                              <div class="method-source-code" id="min_by-source">
            <pre>static VALUE
enum_min_by(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo;
    VALUE num;

    rb_check_arity(argc, 0, 1);

    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);

    if (argc &amp;&amp; !NIL_P(num = argv[0]))
        return rb_nmin_run(obj, num, 1, 0, 0);

    memo = MEMO_NEW(Qundef, Qnil, 0);
    rb_block_call(obj, id_each, 0, 0, min_by_i, (VALUE)memo);
    return memo-&gt;v2;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-minmax" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          minmax               &rarr; [minimum, maximum]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          minmax {|a, b| ... } &rarr; [minimum, maximum]
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns a 2-element array containing the minimum and maximum elements according to a given criterion. The ordering of equal elements is indeterminate and may be unstable.</p>

<p>With no argument and no block, returns the minimum and maximum elements, using the elements’ own method <code>&lt;=&gt;</code> for comparison:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">minmax</span>                   <span class="ruby-comment"># =&gt; [1, 4]</span>
(<span class="ruby-value">-4</span><span class="ruby-operator">..</span><span class="ruby-value">-1</span>).<span class="ruby-identifier">minmax</span>                 <span class="ruby-comment"># =&gt; [-4, -1]</span>
<span class="ruby-node">%w[d c b a]</span>.<span class="ruby-identifier">minmax</span>              <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;d&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">minmax</span> <span class="ruby-comment"># =&gt; [[:bar, 1], [:foo, 0]]</span>
[].<span class="ruby-identifier">minmax</span>                       <span class="ruby-comment"># =&gt; [nil, nil]</span>
</pre>

<p>With a block given, returns the minimum and maximum elements as determined by the block:</p>

<pre class="ruby"><span class="ruby-node">%w[xxx x xxxx xx]</span>.<span class="ruby-identifier">minmax</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">size</span> } <span class="ruby-comment"># =&gt; [&quot;x&quot;, &quot;xxxx&quot;]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">minmax</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">pair1</span>, <span class="ruby-identifier">pair2</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pair1</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">pair2</span>[<span class="ruby-value">1</span>] }
<span class="ruby-comment"># =&gt; [[:foo, 0], [:baz, 2]]</span>
[].<span class="ruby-identifier">minmax</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">b</span> }                          <span class="ruby-comment"># =&gt; [nil, nil]</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-min"><code>min</code></a>, <a href="Enumerable.html#method-i-max"><code>max</code></a>, <a href="Enumerable.html#method-i-minmax_by"><code>minmax_by</code></a>.</p>

                              <div class="method-source-code" id="minmax-source">
            <pre>static VALUE
enum_minmax(VALUE obj)
{
    VALUE memo;
    struct minmax_t *m = NEW_MEMO_FOR(struct minmax_t, memo);

    m-&gt;min = Qundef;
    m-&gt;last = Qundef;
    if (rb_block_given_p()) {
        rb_block_call(obj, id_each, 0, 0, minmax_ii, memo);
        if (!UNDEF_P(m-&gt;last))
            minmax_ii_update(m-&gt;last, m-&gt;last, m);
    }
    else {
        rb_block_call(obj, id_each, 0, 0, minmax_i, memo);
        if (!UNDEF_P(m-&gt;last))
            minmax_i_update(m-&gt;last, m-&gt;last, m);
    }
    if (!UNDEF_P(m-&gt;min)) {
        return rb_assoc_new(m-&gt;min, m-&gt;max);
    }
    return rb_assoc_new(Qnil, Qnil);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-minmax_by" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          minmax_by {|element| ... } &rarr; [minimum, maximum]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          minmax_by                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns a 2-element array containing the elements for which the block returns minimum and maximum values:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">minmax_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }
<span class="ruby-comment"># =&gt; [4, 1]</span>
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">minmax_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span>.<span class="ruby-identifier">ord</span> }
<span class="ruby-comment"># =&gt; [&quot;d&quot;, &quot;a&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">minmax_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">value</span> }
<span class="ruby-comment"># =&gt; [[:baz, 2], [:foo, 0]]</span>
[].<span class="ruby-identifier">minmax_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">element</span> }
<span class="ruby-comment"># =&gt; [nil, nil]</span>
</pre>

<p>Returns an <a href="Enumerator.html"><code>Enumerator</code></a> if no block is given.</p>

<p>Related: <a href="Enumerable.html#method-i-max_by"><code>max_by</code></a>, <a href="Enumerable.html#method-i-minmax"><code>minmax</code></a>, <a href="Enumerable.html#method-i-min_by"><code>min_by</code></a>.</p>

                              <div class="method-source-code" id="minmax_by-source">
            <pre>static VALUE
enum_minmax_by(VALUE obj)
{
    VALUE memo;
    struct minmax_by_t *m = NEW_MEMO_FOR(struct minmax_by_t, memo);

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    m-&gt;min_bv = Qundef;
    m-&gt;max_bv = Qundef;
    m-&gt;min = Qnil;
    m-&gt;max = Qnil;
    m-&gt;last_bv = Qundef;
    m-&gt;last = Qundef;
    rb_block_call(obj, id_each, 0, 0, minmax_by_i, memo);
    if (!UNDEF_P(m-&gt;last_bv))
        minmax_by_i_update(m-&gt;last_bv, m-&gt;last_bv, m-&gt;last, m-&gt;last, m);
    m = MEMO_FOR(struct minmax_by_t, memo);
    return rb_assoc_new(m-&gt;min, m-&gt;max);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-none-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          none?                  &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          none?(pattern)         &rarr; true or false
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          none? {|element| ... } &rarr; true or false
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns whether no element meets a given criterion.</p>

<p>With no argument and no block, returns whether no element is truthy:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">none?</span>           <span class="ruby-comment"># =&gt; false</span>
[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>].<span class="ruby-identifier">none?</span>     <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>}.<span class="ruby-identifier">none?</span>         <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>}.<span class="ruby-identifier">none?</span> <span class="ruby-comment"># =&gt; false</span>
[].<span class="ruby-identifier">none?</span>               <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>With argument <code>pattern</code> and no block, returns whether for no element <code>element</code>, <code>pattern === element</code>:</p>

<pre class="ruby">[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-value">1.1</span>].<span class="ruby-identifier">none?</span>(<span class="ruby-constant">Integer</span>)      <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">none?</span>(<span class="ruby-regexp">/m/</span>)        <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">none?</span>(<span class="ruby-regexp">/foo/</span>)      <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">none?</span>(<span class="ruby-string">&#39;ba&#39;</span>)       <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">none?</span>(<span class="ruby-constant">Hash</span>)  <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>}.<span class="ruby-identifier">none?</span>(<span class="ruby-constant">Array</span>)                 <span class="ruby-comment"># =&gt; false</span>
[].<span class="ruby-identifier">none?</span>(<span class="ruby-constant">Integer</span>)                     <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>With a block given, returns whether the block returns a truthy value for no element:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">none?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1</span> }                     <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">none?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> }                     <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">none?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">0</span> }  <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">none?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1</span> } <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-one-3F"><code>one?</code></a>, <a href="Enumerable.html#method-i-all-3F"><code>all?</code></a>, <a href="Enumerable.html#method-i-any-3F"><code>any?</code></a>.</p>

                              <div class="method-source-code" id="none-3F-source">
            <pre>static VALUE
enum_none(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo = MEMO_ENUM_NEW(Qtrue);

    WARN_UNUSED_BLOCK(argc);
    rb_block_call(obj, id_each, 0, 0, ENUMFUNC(none), (VALUE)memo);
    return memo-&gt;v1;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-one-3F" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          one?                  &rarr; true or false
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          one?(pattern)         &rarr; true or false
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          one? {|element| ... } &rarr; true or false
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns whether exactly one element meets a given criterion.</p>

<p>With no argument and no block, returns whether exactly one element is truthy:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">1</span>).<span class="ruby-identifier">one?</span>           <span class="ruby-comment"># =&gt; true</span>
[<span class="ruby-value">1</span>, <span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>].<span class="ruby-identifier">one?</span>  <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">one?</span>           <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>}.<span class="ruby-identifier">one?</span>         <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>}.<span class="ruby-identifier">one?</span> <span class="ruby-comment"># =&gt; false</span>
[].<span class="ruby-identifier">one?</span>               <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>With argument <code>pattern</code> and no block, returns whether for exactly one element <code>element</code>, <code>pattern === element</code>:</p>

<pre class="ruby">[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-value">0</span>].<span class="ruby-identifier">one?</span>(<span class="ruby-constant">Integer</span>)        <span class="ruby-comment"># =&gt; true</span>
[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-value">0</span>].<span class="ruby-identifier">one?</span>(<span class="ruby-constant">Numeric</span>)        <span class="ruby-comment"># =&gt; true</span>
[<span class="ruby-keyword">nil</span>, <span class="ruby-keyword">false</span>, <span class="ruby-value">0</span>].<span class="ruby-identifier">one?</span>(<span class="ruby-constant">Float</span>)          <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">one?</span>(<span class="ruby-regexp">/m/</span>)        <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">one?</span>(<span class="ruby-regexp">/foo/</span>)      <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-node">%w[bar baz bat bam]</span>.<span class="ruby-identifier">one?</span>(<span class="ruby-string">&#39;ba&#39;</span>)       <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">one?</span>(<span class="ruby-constant">Array</span>) <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>}.<span class="ruby-identifier">one?</span>(<span class="ruby-constant">Array</span>)                 <span class="ruby-comment"># =&gt; true</span>
[].<span class="ruby-identifier">one?</span>(<span class="ruby-constant">Integer</span>)                     <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>With a block given, returns whether the block returns a truthy value for exactly one element:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">one?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> }                     <span class="ruby-comment"># =&gt; true</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">one?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1</span> }                     <span class="ruby-comment"># =&gt; false</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">one?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">1</span> }  <span class="ruby-comment"># =&gt; true</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">one?</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> } <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Related: <a href="Enumerable.html#method-i-none-3F"><code>none?</code></a>, <a href="Enumerable.html#method-i-all-3F"><code>all?</code></a>, <a href="Enumerable.html#method-i-any-3F"><code>any?</code></a>.</p>

                              <div class="method-source-code" id="one-3F-source">
            <pre>static VALUE
enum_one(int argc, VALUE *argv, VALUE obj)
{
    struct MEMO *memo = MEMO_ENUM_NEW(Qundef);
    VALUE result;

    WARN_UNUSED_BLOCK(argc);
    rb_block_call(obj, id_each, 0, 0, ENUMFUNC(one), (VALUE)memo);
    result = memo-&gt;v1;
    if (UNDEF_P(result)) return Qfalse;
    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-partition" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          partition {|element| ... } &rarr; [true_array, false_array]
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          partition &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With a block given, returns an array of two arrays:</p>
<ul><li>
<p>The first having those elements for which the block returns a truthy value.</p>
</li><li>
<p>The other having all other elements.</p>
</li></ul>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-identifier">p</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">partition</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">even?</span> }
<span class="ruby-identifier">p</span> <span class="ruby-comment"># =&gt; [[2, 4], [1, 3]]</span>
<span class="ruby-identifier">p</span> = (<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">partition</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">&lt;</span> <span class="ruby-string">&#39;c&#39;</span> }
<span class="ruby-identifier">p</span> <span class="ruby-comment"># =&gt; [[&quot;a&quot;, &quot;b&quot;], [&quot;c&quot;, &quot;d&quot;]]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bat:</span> <span class="ruby-value">3</span>}
<span class="ruby-identifier">p</span> = <span class="ruby-identifier">h</span>.<span class="ruby-identifier">partition</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;b&#39;</span>) }
<span class="ruby-identifier">p</span> <span class="ruby-comment"># =&gt; [[[:bar, 1], [:baz, 2], [:bat, 3]], [[:foo, 0]]]</span>
<span class="ruby-identifier">p</span> = <span class="ruby-identifier">h</span>.<span class="ruby-identifier">partition</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> }
<span class="ruby-identifier">p</span> <span class="ruby-comment"># =&gt; [[[:foo, 0], [:bar, 1]], [[:baz, 2], [:bat, 3]]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>Related: <a href="Enumerable.html#method-i-group_by"><code>Enumerable#group_by</code></a>.</p>

                              <div class="method-source-code" id="partition-source">
            <pre>static VALUE
enum_partition(VALUE obj)
{
    struct MEMO *memo;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    memo = MEMO_NEW(rb_ary_new(), rb_ary_new(), 0);
    rb_block_call(obj, id_each, 0, 0, partition_i, (VALUE)memo);

    return rb_assoc_new(memo-&gt;v1, memo-&gt;v2);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-reduce" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">reduce</span><span
                                class="method-args">(p1 = v1, p2 = v2)</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an object formed from operands via either:</p>
<ul><li>
<p>A method named by <code>symbol</code>.</p>
</li><li>
<p>A block to which each operand is passed.</p>
</li></ul>

<p>With method-name argument <code>symbol</code>, combines operands using the method:</p>

<pre class="ruby"><span class="ruby-comment"># Sum, without initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)     <span class="ruby-comment"># =&gt; 10</span>
<span class="ruby-comment"># Sum, with initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">10</span>, <span class="ruby-value">:+</span>) <span class="ruby-comment"># =&gt; 20</span>
</pre>

<p>With a block, passes each operand to the block:</p>

<pre class="ruby"><span class="ruby-comment"># Sum of squares, without initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">sum</span>, <span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">sum</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">n</span><span class="ruby-operator">*</span><span class="ruby-identifier">n</span> }    <span class="ruby-comment"># =&gt; 30</span>
<span class="ruby-comment"># Sum of squares, with initial_operand.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">2</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sum</span>, <span class="ruby-identifier">n</span><span class="ruby-operator">|</span> <span class="ruby-identifier">sum</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">n</span><span class="ruby-operator">*</span><span class="ruby-identifier">n</span> } <span class="ruby-comment"># =&gt; 32</span>
</pre>

<p><strong>Operands</strong></p>

<p>If argument <code>initial_operand</code> is not given, the operands for <code>inject</code> are simply the elements of <code>self</code>. Example calls and their operands:</p>
<ul><li><dl class="rdoc-list note-list"><dt><code>(1..4).inject(:+)</code>
<dd>
<p><code>[1, 2, 3, 4]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(1...4).inject(:+)</code>
<dd>
<p><code>[1, 2, 3]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;..&#39;d&#39;).inject(:+)</code>
<dd>
<p><code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;...&#39;d&#39;).inject(:+)</code>
<dd>
<p><code>[&#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</code>.</p>
</dd></dl>
</li></ul>

<p>Examples with first operand (which is <code>self.first</code>) of various types:</p>

<pre class="ruby"><span class="ruby-comment"># Integer.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)                <span class="ruby-comment"># =&gt; 10</span>
<span class="ruby-comment"># Float.</span>
[<span class="ruby-value">1.0</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">4</span>].<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)        <span class="ruby-comment"># =&gt; 10.0</span>
<span class="ruby-comment"># Character.</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)            <span class="ruby-comment"># =&gt; &quot;abcd&quot;</span>
<span class="ruby-comment"># Complex.</span>
[<span class="ruby-constant">Complex</span>(<span class="ruby-value">1</span>, <span class="ruby-value">2</span>), <span class="ruby-value">3</span>, <span class="ruby-value">4</span>].<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>) <span class="ruby-comment"># =&gt; (8+2i)</span>
</pre>

<p>If argument <code>initial_operand</code> is given, the operands for <code>inject</code> are that value plus the elements of <code>self</code>. Example calls their operands:</p>
<ul><li><dl class="rdoc-list note-list"><dt><code>(1..4).inject(10, :+)</code>
<dd>
<p><code>[10, 1, 2, 3, 4]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(1...4).inject(10, :+)</code>
<dd>
<p><code>[10, 1, 2, 3]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;..&#39;d&#39;).inject(&#39;e&#39;, :+)</code>
<dd>
<p><code>[&#39;e&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;]</code>.</p>
</dd></dl>
</li><li><dl class="rdoc-list note-list"><dt><code>(&#39;a&#39;...&#39;d&#39;).inject(&#39;e&#39;, :+)</code>
<dd>
<p><code>[&#39;e&#39;, &#39;a&#39;, &#39;b&#39;, &#39;c&#39;]</code>.</p>
</dd></dl>
</li></ul>

<p>Examples with <code>initial_operand</code> of various types:</p>

<pre class="ruby"><span class="ruby-comment"># Integer.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">2</span>, <span class="ruby-value">:+</span>)               <span class="ruby-comment"># =&gt; 12</span>
<span class="ruby-comment"># Float.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">2.0</span>, <span class="ruby-value">:+</span>)             <span class="ruby-comment"># =&gt; 12.0</span>
<span class="ruby-comment"># String.</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;foo&#39;</span>, <span class="ruby-value">:+</span>)       <span class="ruby-comment"># =&gt; &quot;fooabcd&quot;</span>
<span class="ruby-comment"># Array.</span>
<span class="ruby-node">%w[a b c]</span>.<span class="ruby-identifier">inject</span>([<span class="ruby-string">&#39;x&#39;</span>], <span class="ruby-value">:push</span>)     <span class="ruby-comment"># =&gt; [&quot;x&quot;, &quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
<span class="ruby-comment"># Complex.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-constant">Complex</span>(<span class="ruby-value">2</span>, <span class="ruby-value">2</span>), <span class="ruby-value">:+</span>)   <span class="ruby-comment"># =&gt; (12+2i)</span>
</pre>

<p><strong>Combination by Given Method</strong></p>

<p>If the method-name argument <code>symbol</code> is given, the operands are combined by that method:</p>
<ul><li>
<p>The first and second operands are combined.</p>
</li><li>
<p>That result is combined with the third operand.</p>
</li><li>
<p>That result is combined with the fourth operand.</p>
</li><li>
<p>And so on.</p>
</li></ul>

<p>The return value from <code>inject</code> is the result of the last combination.</p>

<p>This call to <code>inject</code> computes the sum of the operands:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>) <span class="ruby-comment"># =&gt; 10</span>
</pre>

<p>Examples with various methods:</p>

<pre class="ruby"><span class="ruby-comment"># Integer addition.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:+</span>)                <span class="ruby-comment"># =&gt; 10</span>
<span class="ruby-comment"># Integer multiplication.</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value">:*</span>)                <span class="ruby-comment"># =&gt; 24</span>
<span class="ruby-comment"># Character range concatenation.</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">:+</span>)        <span class="ruby-comment"># =&gt; &quot;abcd&quot;</span>
<span class="ruby-comment"># String array concatenation.</span>
<span class="ruby-node">%w[foo bar baz]</span>.<span class="ruby-identifier">inject</span>(<span class="ruby-string">&#39;&#39;</span>, <span class="ruby-value">:+</span>)   <span class="ruby-comment"># =&gt; &quot;foobarbaz&quot;</span>
<span class="ruby-comment"># Hash update.</span>
<span class="ruby-identifier">h</span> = [{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>}, {<span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}, {<span class="ruby-value">bat:</span> <span class="ruby-value">3</span>}].<span class="ruby-identifier">inject</span>(<span class="ruby-value">:update</span>)
<span class="ruby-identifier">h</span> <span class="ruby-comment"># =&gt; {:foo=&gt;0, :bar=&gt;1, :baz=&gt;2, :bat=&gt;3}</span>
<span class="ruby-comment"># Hash conversion to nested arrays.</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>}.<span class="ruby-identifier">inject</span>([], <span class="ruby-value">:push</span>)
<span class="ruby-identifier">h</span> <span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1]]</span>
</pre>

<p><strong>Combination by Given Block</strong></p>

<p>If a block is given, the operands are passed to the block:</p>
<ul><li>
<p>The first call passes the first and second operands.</p>
</li><li>
<p>The second call passes the result of the first call, along with the third operand.</p>
</li><li>
<p>The third call passes the result of the second call, along with the fourth operand.</p>
</li><li>
<p>And so on.</p>
</li></ul>

<p>The return value from <code>inject</code> is the return value from the last block call.</p>

<p>This call to <code>inject</code> gives a block that writes the memo and element, and also sums the elements:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">inject</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">memo</span>, <span class="ruby-identifier">element</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">p</span> <span class="ruby-node">&quot;Memo: #{memo}; element: #{element}&quot;</span>
  <span class="ruby-identifier">memo</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">element</span>
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; 10</span>
</pre>

<p>Output:</p>

<pre class="ruby"><span class="ruby-string">&quot;Memo: 1; element: 2&quot;</span>
<span class="ruby-string">&quot;Memo: 3; element: 3&quot;</span>
<span class="ruby-string">&quot;Memo: 6; element: 4&quot;</span>
</pre>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-inject">inject</a>
                            </div>
                          </div>

                  <div id="method-i-reject" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          reject {|element| ... } &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          reject &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array of objects rejected by the block.</p>

<p>With a block given, calls the block with successive elements; returns an array of those elements for which the block returns <code>nil</code> or <code>false</code>:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">reject</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">*</span> <span class="ruby-value">2</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">even?</span> }                             <span class="ruby-comment"># =&gt; [1, 3, 5, 7, 9]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">reject</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">odd?</span> } <span class="ruby-comment"># =&gt; {:foo=&gt;0, :baz=&gt;2}</span>
</pre>

<p>When no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>Related: <a href="Enumerable.html#method-i-select"><code>select</code></a>.</p>

                              <div class="method-source-code" id="reject-source">
            <pre>static VALUE
enum_reject(VALUE obj)
{
    VALUE ary;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    ary = rb_ary_new();
    rb_block_call(obj, id_each, 0, 0, reject_i, ary);

    return ary;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-reverse_each" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          reverse_each(*args) {|element| ... } &rarr;  self
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          reverse_each(*args)                  &rarr;  enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With a block given, calls the block with each element, but in reverse order; returns <code>self</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = []
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">reverse_each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-operator">-</span><span class="ruby-identifier">element</span>) } <span class="ruby-comment"># =&gt; 1..4</span>
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [-4, -3, -2, -1]</span>

<span class="ruby-identifier">a</span> = []
<span class="ruby-node">%w[a b c d]</span>.<span class="ruby-identifier">reverse_each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) }
<span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]</span>
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [&quot;d&quot;, &quot;c&quot;, &quot;b&quot;, &quot;a&quot;]</span>

<span class="ruby-identifier">a</span> = []
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">reverse_each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">element</span>) }
<span class="ruby-comment"># =&gt; {:foo=&gt;0, :bar=&gt;1, :baz=&gt;2}</span>
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; [[:baz, 2], [:bar, 1], [:foo, 0]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="reverse_each-source">
            <pre>static VALUE
enum_reverse_each(int argc, VALUE *argv, VALUE obj)
{
    VALUE ary;
    long len;

    RETURN_SIZED_ENUMERATOR(obj, argc, argv, enum_size);

    ary = enum_to_a(argc, argv, obj);

    len = RARRAY_LEN(ary);
    while (len--) {
        long nlen;
        rb_yield(RARRAY_AREF(ary, len));
        nlen = RARRAY_LEN(ary);
        if (nlen &lt; len) {
            len = nlen;
        }
    }

    return obj;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-select" class="method-detail method-alias">
                      <div class="method-heading">
                        <span class="method-callseq">
                          select {|element| ... } &rarr; array
                              </span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          select &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array containing elements selected by the block.</p>

<p>With a block given, calls the block with successive elements; returns an array of those elements for which the block returns a truthy value:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">9</span>).<span class="ruby-identifier">select</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">element</span> <span class="ruby-operator">%</span> <span class="ruby-value">3</span> <span class="ruby-operator">==</span> <span class="ruby-value">0</span> } <span class="ruby-comment"># =&gt; [0, 3, 6, 9]</span>
<span class="ruby-identifier">a</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">select</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&#39;b&#39;</span>) }
<span class="ruby-identifier">a</span> <span class="ruby-comment"># =&gt; {:bar=&gt;1, :baz=&gt;2}</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>Related: <a href="Enumerable.html#method-i-reject"><code>reject</code></a>.</p>

                            </div>


                            <div class="aliases">
                              Alias for: <a href="Enumerable.html#method-i-find_all">find_all</a>
                            </div>
                          </div>

                  <div id="method-i-slice_after" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          slice_after(pattern)       &rarr; an_enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          slice_after { |elt| bool } &rarr; an_enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Creates an enumerator for each chunked elements. The ends of chunks are defined by <em>pattern</em> and the block.</p>

<p>If <code><em>pattern</em> === <em>elt</em></code> returns <code>true</code> or the block returns <code>true</code> for the element, the element is end of a chunk.</p>

<p>The <code>===</code> and <em>block</em> is called from the first element to the last element of <em>enum</em>.</p>

<p>The result enumerator yields the chunked elements as an array. So <code>each</code> method can be called as follows:</p>

<pre>enum.slice_after(pattern).each { |ary| ... }
enum.slice_after { |elt| bool }.each { |ary| ... }</pre>

<p>Other methods of the <a href="Enumerator.html"><code>Enumerator</code></a> class and <a href="Enumerable.html"><code>Enumerable</code></a> module, such as <code>map</code>, etc., are also usable.</p>

<p>For example, continuation lines (lines end with backslash) can be concatenated as follows:</p>

<pre class="ruby"><span class="ruby-identifier">lines</span> = [<span class="ruby-string">&quot;foo\n&quot;</span>, <span class="ruby-string">&quot;bar\\\n&quot;</span>, <span class="ruby-string">&quot;baz\n&quot;</span>, <span class="ruby-string">&quot;\n&quot;</span>, <span class="ruby-string">&quot;qux\n&quot;</span>]
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">lines</span>.<span class="ruby-identifier">slice_after</span>(<span class="ruby-regexp">/(?&lt;!\\)\n\z/</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_a</span>
<span class="ruby-comment">#=&gt; [[&quot;foo\n&quot;], [&quot;bar\\\n&quot;, &quot;baz\n&quot;], [&quot;\n&quot;], [&quot;qux\n&quot;]]</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">ll</span><span class="ruby-operator">|</span> <span class="ruby-identifier">ll</span>[<span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-value">-1</span>].<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">l</span><span class="ruby-operator">|</span> <span class="ruby-identifier">l</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-regexp">/\\\n\z/</span>, <span class="ruby-string">&quot;&quot;</span>) }.<span class="ruby-identifier">join</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">ll</span>.<span class="ruby-identifier">last</span> }
<span class="ruby-comment">#=&gt;[&quot;foo\n&quot;, &quot;barbaz\n&quot;, &quot;\n&quot;, &quot;qux\n&quot;]</span>
</pre>

                              <div class="method-source-code" id="slice_after-source">
            <pre>static VALUE
enum_slice_after(int argc, VALUE *argv, VALUE enumerable)
{
    VALUE enumerator;
    VALUE pat = Qnil, pred = Qnil;

    if (rb_block_given_p()) {
        if (0 &lt; argc)
            rb_raise(rb_eArgError, &quot;both pattern and block are given&quot;);
        pred = rb_block_proc();
    }
    else {
        rb_scan_args(argc, argv, &quot;1&quot;, &amp;pat);
    }

    enumerator = rb_obj_alloc(rb_cEnumerator);
    rb_ivar_set(enumerator, id_sliceafter_enum, enumerable);
    rb_ivar_set(enumerator, id_sliceafter_pat, pat);
    rb_ivar_set(enumerator, id_sliceafter_pred, pred);

    rb_block_call(enumerator, idInitialize, 0, 0, sliceafter_i, enumerator);
    return enumerator;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-slice_before" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          slice_before(pattern)       &rarr; enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          slice_before {|elt| ... } &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With argument <code>pattern</code>, returns an enumerator that uses the pattern to partition elements into arrays (“slices”). An element begins a new slice if <code>element === pattern</code> (or if it is the first element).</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-node">%w[foo bar fop for baz fob fog bam foy]</span>
<span class="ruby-identifier">e</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">slice_before</span>(<span class="ruby-regexp">/ba/</span>) <span class="ruby-comment"># =&gt; #&lt;Enumerator: ...&gt;</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">array</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">array</span> }
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>]
[<span class="ruby-string">&quot;bar&quot;</span>, <span class="ruby-string">&quot;fop&quot;</span>, <span class="ruby-string">&quot;for&quot;</span>]
[<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-string">&quot;fob&quot;</span>, <span class="ruby-string">&quot;fog&quot;</span>]
[<span class="ruby-string">&quot;bam&quot;</span>, <span class="ruby-string">&quot;foy&quot;</span>]
</pre>

<p>With a block, returns an enumerator that uses the block to partition elements into arrays. An element begins a new slice if its block return is a truthy value (or if it is the first element):</p>

<pre class="ruby"><span class="ruby-identifier">e</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">20</span>).<span class="ruby-identifier">slice_before</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">%</span> <span class="ruby-value">4</span> <span class="ruby-operator">==</span> <span class="ruby-value">2</span> } <span class="ruby-comment"># =&gt; #&lt;Enumerator: ...&gt;</span>
<span class="ruby-identifier">e</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">array</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">array</span> }
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">1</span>]
[<span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">4</span>, <span class="ruby-value">5</span>]
[<span class="ruby-value">6</span>, <span class="ruby-value">7</span>, <span class="ruby-value">8</span>, <span class="ruby-value">9</span>]
[<span class="ruby-value">10</span>, <span class="ruby-value">11</span>, <span class="ruby-value">12</span>, <span class="ruby-value">13</span>]
[<span class="ruby-value">14</span>, <span class="ruby-value">15</span>, <span class="ruby-value">16</span>, <span class="ruby-value">17</span>]
[<span class="ruby-value">18</span>, <span class="ruby-value">19</span>, <span class="ruby-value">20</span>]
</pre>

<p>Other methods of the <a href="Enumerator.html"><code>Enumerator</code></a> class and <a href="Enumerable.html"><code>Enumerable</code></a> module, such as <code>to_a</code>, <code>map</code>, etc., are also usable.</p>

<p>For example, iteration over ChangeLog entries can be implemented as follows:</p>

<pre class="ruby"><span class="ruby-comment"># iterate over ChangeLog entries.</span>
<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;ChangeLog&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">f</span>.<span class="ruby-identifier">slice_before</span>(<span class="ruby-regexp">/\A\S/</span>).<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pp</span> <span class="ruby-identifier">e</span> }
}

<span class="ruby-comment"># same as above.  block is used instead of pattern argument.</span>
<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;ChangeLog&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">f</span>.<span class="ruby-identifier">slice_before</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> <span class="ruby-regexp">/\A\S/</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">line</span> }.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span> <span class="ruby-identifier">pp</span> <span class="ruby-identifier">e</span> }
}
</pre>

<p>“svn proplist -R” produces multiline output for each file. They can be chunked as follows:</p>

<pre class="ruby"><span class="ruby-constant">IO</span>.<span class="ruby-identifier">popen</span>([{<span class="ruby-string">&quot;LC_ALL&quot;</span><span class="ruby-operator">=&gt;</span><span class="ruby-string">&quot;C&quot;</span>}, <span class="ruby-string">&quot;svn&quot;</span>, <span class="ruby-string">&quot;proplist&quot;</span>, <span class="ruby-string">&quot;-R&quot;</span>]) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">f</span>.<span class="ruby-identifier">lines</span>.<span class="ruby-identifier">slice_before</span>(<span class="ruby-regexp">/\AProp/</span>).<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">lines</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">lines</span> }
}
<span class="ruby-comment">#=&gt; [&quot;Properties on &#39;.&#39;:\n&quot;, &quot;  svn:ignore\n&quot;, &quot;  svk:merge\n&quot;]</span>
<span class="ruby-comment">#   [&quot;Properties on &#39;goruby.c&#39;:\n&quot;, &quot;  svn:eol-style\n&quot;]</span>
<span class="ruby-comment">#   [&quot;Properties on &#39;complex.c&#39;:\n&quot;, &quot;  svn:mime-type\n&quot;, &quot;  svn:eol-style\n&quot;]</span>
<span class="ruby-comment">#   [&quot;Properties on &#39;regparse.c&#39;:\n&quot;, &quot;  svn:eol-style\n&quot;]</span>
<span class="ruby-comment">#   ...</span>
</pre>

<p>If the block needs to maintain state over multiple elements, local variables can be used. For example, three or more consecutive increasing numbers can be squashed as follows (see <code>chunk_while</code> for a better way):</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">4</span>, <span class="ruby-value">6</span>, <span class="ruby-value">7</span>, <span class="ruby-value">9</span>]
<span class="ruby-identifier">prev</span> = <span class="ruby-identifier">a</span>[<span class="ruby-value">0</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">slice_before</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">e</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">prev</span>, <span class="ruby-identifier">prev2</span> = <span class="ruby-identifier">e</span>, <span class="ruby-identifier">prev</span>
  <span class="ruby-identifier">prev2</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">e</span>
}.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">es</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">es</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;=</span> <span class="ruby-value">2</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">es</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;,&quot;</span>) <span class="ruby-operator">:</span> <span class="ruby-node">&quot;#{es.first}-#{es.last}&quot;</span>
}.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;,&quot;</span>)
<span class="ruby-comment">#=&gt; &quot;0,2-4,6,7,9&quot;</span>
</pre>

<p>However local variables should be used carefully if the result enumerator is enumerated twice or more. The local variables should be initialized for each enumeration. <a href="Enumerator.html#method-c-new"><code>Enumerator.new</code></a> can be used to do it.</p>

<pre class="ruby"><span class="ruby-comment"># Word wrapping.  This assumes all characters have same width.</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">wordwrap</span>(<span class="ruby-identifier">words</span>, <span class="ruby-identifier">maxwidth</span>)
  <span class="ruby-constant">Enumerator</span>.<span class="ruby-identifier">new</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">y</span><span class="ruby-operator">|</span>
    <span class="ruby-comment"># cols is initialized in Enumerator.new.</span>
    <span class="ruby-identifier">cols</span> = <span class="ruby-value">0</span>
    <span class="ruby-identifier">words</span>.<span class="ruby-identifier">slice_before</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">w</span><span class="ruby-operator">|</span>
      <span class="ruby-identifier">cols</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">cols</span> <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>
      <span class="ruby-identifier">cols</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">w</span>.<span class="ruby-identifier">length</span>
      <span class="ruby-keyword">if</span> <span class="ruby-identifier">maxwidth</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">cols</span>
        <span class="ruby-identifier">cols</span> = <span class="ruby-identifier">w</span>.<span class="ruby-identifier">length</span>
        <span class="ruby-keyword">true</span>
      <span class="ruby-keyword">else</span>
        <span class="ruby-keyword">false</span>
      <span class="ruby-keyword">end</span>
    }.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">ws</span><span class="ruby-operator">|</span> <span class="ruby-identifier">y</span>.<span class="ruby-identifier">yield</span> <span class="ruby-identifier">ws</span> }
  }
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">text</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">20</span>).<span class="ruby-identifier">to_a</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot; &quot;</span>)
<span class="ruby-identifier">enum</span> = <span class="ruby-identifier">wordwrap</span>(<span class="ruby-identifier">text</span>.<span class="ruby-identifier">split</span>(<span class="ruby-regexp">/\s+/</span>), <span class="ruby-value">10</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;-&quot;</span><span class="ruby-operator">*</span><span class="ruby-value">10</span>
<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">ws</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-identifier">ws</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot; &quot;</span>) } <span class="ruby-comment"># first enumeration.</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;-&quot;</span><span class="ruby-operator">*</span><span class="ruby-value">10</span>
<span class="ruby-identifier">enum</span>.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">ws</span><span class="ruby-operator">|</span> <span class="ruby-identifier">puts</span> <span class="ruby-identifier">ws</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot; &quot;</span>) } <span class="ruby-comment"># second enumeration generates same result as the first.</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;-&quot;</span><span class="ruby-operator">*</span><span class="ruby-value">10</span>
<span class="ruby-comment">#=&gt; ----------</span>
<span class="ruby-comment">#   1 2 3 4 5</span>
<span class="ruby-comment">#   6 7 8 9 10</span>
<span class="ruby-comment">#   11 12 13</span>
<span class="ruby-comment">#   14 15 16</span>
<span class="ruby-comment">#   17 18 19</span>
<span class="ruby-comment">#   20</span>
<span class="ruby-comment">#   ----------</span>
<span class="ruby-comment">#   1 2 3 4 5</span>
<span class="ruby-comment">#   6 7 8 9 10</span>
<span class="ruby-comment">#   11 12 13</span>
<span class="ruby-comment">#   14 15 16</span>
<span class="ruby-comment">#   17 18 19</span>
<span class="ruby-comment">#   20</span>
<span class="ruby-comment">#   ----------</span>
</pre>

<p>mbox contains series of mails which start with Unix From line. So each mail can be extracted by slice before Unix From line.</p>

<pre class="ruby"><span class="ruby-comment"># parse mbox</span>
<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;mbox&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">f</span>.<span class="ruby-identifier">slice_before</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">line</span>.<span class="ruby-identifier">start_with?</span> <span class="ruby-string">&quot;From &quot;</span>
  }.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">mail</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">unix_from</span> = <span class="ruby-identifier">mail</span>.<span class="ruby-identifier">shift</span>
    <span class="ruby-identifier">i</span> = <span class="ruby-identifier">mail</span>.<span class="ruby-identifier">index</span>(<span class="ruby-string">&quot;\n&quot;</span>)
    <span class="ruby-identifier">header</span> = <span class="ruby-identifier">mail</span>[<span class="ruby-value">0</span><span class="ruby-operator">...</span><span class="ruby-identifier">i</span>]
    <span class="ruby-identifier">body</span> = <span class="ruby-identifier">mail</span>[(<span class="ruby-identifier">i</span><span class="ruby-value">+1</span>)<span class="ruby-operator">..</span><span class="ruby-value">-1</span>]
    <span class="ruby-identifier">body</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">body</span>.<span class="ruby-identifier">last</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;\n&quot;</span>
    <span class="ruby-identifier">fields</span> = <span class="ruby-identifier">header</span>.<span class="ruby-identifier">slice_before</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span> <span class="ruby-operator">!</span><span class="ruby-string">&quot; \t&quot;</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">line</span>[<span class="ruby-value">0</span>]) }.<span class="ruby-identifier">to_a</span>
    <span class="ruby-identifier">p</span> <span class="ruby-identifier">unix_from</span>
    <span class="ruby-identifier">pp</span> <span class="ruby-identifier">fields</span>
    <span class="ruby-identifier">pp</span> <span class="ruby-identifier">body</span>
  }
}

<span class="ruby-comment"># split mails in mbox (slice before Unix From line after an empty line)</span>
<span class="ruby-identifier">open</span>(<span class="ruby-string">&quot;mbox&quot;</span>) { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">emp</span> = <span class="ruby-keyword">true</span>
  <span class="ruby-identifier">f</span>.<span class="ruby-identifier">slice_before</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">line</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">prevemp</span> = <span class="ruby-identifier">emp</span>
    <span class="ruby-identifier">emp</span> = <span class="ruby-identifier">line</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;\n&quot;</span>
    <span class="ruby-identifier">prevemp</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">line</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-string">&quot;From &quot;</span>)
  }.<span class="ruby-identifier">each</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">mail</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">mail</span>.<span class="ruby-identifier">pop</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">mail</span>.<span class="ruby-identifier">last</span> <span class="ruby-operator">==</span> <span class="ruby-string">&quot;\n&quot;</span>
    <span class="ruby-identifier">pp</span> <span class="ruby-identifier">mail</span>
  }
}
</pre>

                              <div class="method-source-code" id="slice_before-source">
            <pre>static VALUE
enum_slice_before(int argc, VALUE *argv, VALUE enumerable)
{
    VALUE enumerator;

    if (rb_block_given_p()) {
        if (argc != 0)
            rb_error_arity(argc, 0, 0);
        enumerator = rb_obj_alloc(rb_cEnumerator);
        rb_ivar_set(enumerator, id_slicebefore_sep_pred, rb_block_proc());
    }
    else {
        VALUE sep_pat;
        rb_scan_args(argc, argv, &quot;1&quot;, &amp;sep_pat);
        enumerator = rb_obj_alloc(rb_cEnumerator);
        rb_ivar_set(enumerator, id_slicebefore_sep_pat, sep_pat);
    }
    rb_ivar_set(enumerator, id_slicebefore_enumerable, enumerable);
    rb_block_call(enumerator, idInitialize, 0, 0, slicebefore_i, enumerator);
    return enumerator;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-slice_when" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          slice_when {|elt_before, elt_after| bool } &rarr; an_enumerator
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Creates an enumerator for each chunked elements. The beginnings of chunks are defined by the block.</p>

<p>This method splits each chunk using adjacent elements, <em>elt_before</em> and <em>elt_after</em>, in the receiver enumerator. This method split chunks between <em>elt_before</em> and <em>elt_after</em> where the block returns <code>true</code>.</p>

<p>The block is called the length of the receiver enumerator minus one.</p>

<p>The result enumerator yields the chunked elements as an array. So <code>each</code> method can be called as follows:</p>

<pre>enum.slice_when { |elt_before, elt_after| bool }.each { |ary| ... }</pre>

<p>Other methods of the <a href="Enumerator.html"><code>Enumerator</code></a> class and <a href="Enumerable.html"><code>Enumerable</code></a> module, such as <code>to_a</code>, <code>map</code>, etc., are also usable.</p>

<p>For example, one-by-one increasing subsequence can be chunked as follows:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">1</span>,<span class="ruby-value">2</span>,<span class="ruby-value">4</span>,<span class="ruby-value">9</span>,<span class="ruby-value">10</span>,<span class="ruby-value">11</span>,<span class="ruby-value">12</span>,<span class="ruby-value">15</span>,<span class="ruby-value">16</span>,<span class="ruby-value">19</span>,<span class="ruby-value">20</span>,<span class="ruby-value">21</span>]
<span class="ruby-identifier">b</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">slice_when</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">j</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span><span class="ruby-value">+1</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">j</span> }
<span class="ruby-identifier">p</span> <span class="ruby-identifier">b</span>.<span class="ruby-identifier">to_a</span> <span class="ruby-comment">#=&gt; [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]</span>
<span class="ruby-identifier">c</span> = <span class="ruby-identifier">b</span>.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">3</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">:</span> <span class="ruby-node">&quot;#{a.first}-#{a.last}&quot;</span> }
<span class="ruby-identifier">p</span> <span class="ruby-identifier">c</span> <span class="ruby-comment">#=&gt; [[1, 2], [4], &quot;9-12&quot;, [15, 16], &quot;19-21&quot;]</span>
<span class="ruby-identifier">d</span> = <span class="ruby-identifier">c</span>.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot;,&quot;</span>)
<span class="ruby-identifier">p</span> <span class="ruby-identifier">d</span> <span class="ruby-comment">#=&gt; &quot;1,2,4,9-12,15,16,19-21&quot;</span>
</pre>

<p>Near elements (threshold: 6) in sorted array can be chunked as follows:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">3</span>, <span class="ruby-value">11</span>, <span class="ruby-value">14</span>, <span class="ruby-value">25</span>, <span class="ruby-value">28</span>, <span class="ruby-value">29</span>, <span class="ruby-value">29</span>, <span class="ruby-value">41</span>, <span class="ruby-value">55</span>, <span class="ruby-value">57</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">slice_when</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">j</span><span class="ruby-operator">|</span> <span class="ruby-value">6</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">j</span> <span class="ruby-operator">-</span> <span class="ruby-identifier">i</span> }.<span class="ruby-identifier">to_a</span>
<span class="ruby-comment">#=&gt; [[3], [11, 14], [25, 28, 29, 29], [41], [55, 57]]</span>
</pre>

<p>Increasing (non-decreasing) subsequence can be chunked as follows:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">9</span>, <span class="ruby-value">2</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">2</span>, <span class="ruby-value">7</span>, <span class="ruby-value">5</span>, <span class="ruby-value">9</span>, <span class="ruby-value">5</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">slice_when</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">j</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">j</span> }.<span class="ruby-identifier">to_a</span>
<span class="ruby-comment">#=&gt; [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]</span>
</pre>

<p>Adjacent evens and odds can be chunked as follows: (<a href="Enumerable.html#method-i-chunk"><code>Enumerable#chunk</code></a> is another way to do it.)</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">7</span>, <span class="ruby-value">5</span>, <span class="ruby-value">9</span>, <span class="ruby-value">2</span>, <span class="ruby-value">0</span>, <span class="ruby-value">7</span>, <span class="ruby-value">9</span>, <span class="ruby-value">4</span>, <span class="ruby-value">2</span>, <span class="ruby-value">0</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">a</span>.<span class="ruby-identifier">slice_when</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span>, <span class="ruby-identifier">j</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">even?</span> <span class="ruby-operator">!=</span> <span class="ruby-identifier">j</span>.<span class="ruby-identifier">even?</span> }.<span class="ruby-identifier">to_a</span>
<span class="ruby-comment">#=&gt; [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]</span>
</pre>

<p>Paragraphs (non-empty lines with trailing empty lines) can be chunked as follows: (See <a href="Enumerable.html#method-i-chunk"><code>Enumerable#chunk</code></a> to ignore empty lines.)</p>

<pre class="ruby"><span class="ruby-identifier">lines</span> = [<span class="ruby-string">&quot;foo\n&quot;</span>, <span class="ruby-string">&quot;bar\n&quot;</span>, <span class="ruby-string">&quot;\n&quot;</span>, <span class="ruby-string">&quot;baz\n&quot;</span>, <span class="ruby-string">&quot;qux\n&quot;</span>]
<span class="ruby-identifier">p</span> <span class="ruby-identifier">lines</span>.<span class="ruby-identifier">slice_when</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">l1</span>, <span class="ruby-identifier">l2</span><span class="ruby-operator">|</span> <span class="ruby-regexp">/\A\s*\z/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">l1</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-regexp">/\S/</span> <span class="ruby-operator">=~</span> <span class="ruby-identifier">l2</span> }.<span class="ruby-identifier">to_a</span>
<span class="ruby-comment">#=&gt; [[&quot;foo\n&quot;, &quot;bar\n&quot;, &quot;\n&quot;], [&quot;baz\n&quot;, &quot;qux\n&quot;]]</span>
</pre>

<p><a href="Enumerable.html#method-i-chunk_while"><code>Enumerable#chunk_while</code></a> does the same, except splitting when the block returns <code>false</code> instead of <code>true</code>.</p>

                              <div class="method-source-code" id="slice_when-source">
            <pre>static VALUE
enum_slice_when(VALUE enumerable)
{
    VALUE enumerator;
    VALUE pred;

    pred = rb_block_proc();

    enumerator = rb_obj_alloc(rb_cEnumerator);
    rb_ivar_set(enumerator, id_slicewhen_enum, enumerable);
    rb_ivar_set(enumerator, id_slicewhen_pred, pred);
    rb_ivar_set(enumerator, id_slicewhen_inverted, Qfalse);

    rb_block_call(enumerator, idInitialize, 0, 0, slicewhen_i, enumerator);
    return enumerator;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-sort" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sort               &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          sort {|a, b| ... } &rarr; array
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array containing the sorted elements of <code>self</code>. The ordering of equal elements is indeterminate and may be unstable.</p>

<p>With no block given, the sort compares using the elements’ own method <code>&lt;=&gt;</code>:</p>

<pre class="ruby"><span class="ruby-node">%w[b c a d]</span>.<span class="ruby-identifier">sort</span>              <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;, &quot;d&quot;]</span>
{<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}.<span class="ruby-identifier">sort</span> <span class="ruby-comment"># =&gt; [[:bar, 1], [:baz, 2], [:foo, 0]]</span>
</pre>

<p>With a block given, comparisons in the block determine the ordering. The block is called with two elements <code>a</code> and <code>b</code>, and must return:</p>
<ul><li>
<p>A negative integer if <code>a &lt; b</code>.</p>
</li><li>
<p>Zero if <code>a == b</code>.</p>
</li><li>
<p>A positive integer if <code>a &gt; b</code>.</p>
</li></ul>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-node">%w[b c a d]</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">sort</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">b</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">a</span> } <span class="ruby-comment"># =&gt; [&quot;d&quot;, &quot;c&quot;, &quot;b&quot;, &quot;a&quot;]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">sort</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-identifier">b</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">a</span> } <span class="ruby-comment"># =&gt; [[:foo, 0], [:baz, 2], [:bar, 1]]</span>
</pre>

<p>See also <a href="Enumerable.html#method-i-sort_by"><code>sort_by</code></a>. It implements a Schwartzian transform which is useful when key computation or comparison is expensive.</p>

                              <div class="method-source-code" id="sort-source">
            <pre>static VALUE
enum_sort(VALUE obj)
{
    return rb_ary_sort_bang(enum_to_a(0, 0, obj));
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-sort_by" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sort_by {|element| ... } &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          sort_by                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With a block given, returns an array of elements of <code>self</code>, sorted according to the value returned by the block for each element. The ordering of equal elements is indeterminate and may be unstable.</p>

<p>Examples:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = <span class="ruby-node">%w[xx xxx x xxxx]</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">sort_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span> <span class="ruby-identifier">s</span>.<span class="ruby-identifier">size</span> }        <span class="ruby-comment"># =&gt; [&quot;x&quot;, &quot;xx&quot;, &quot;xxx&quot;, &quot;xxxx&quot;]</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">sort_by</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span> <span class="ruby-operator">-</span><span class="ruby-identifier">s</span>.<span class="ruby-identifier">size</span> }       <span class="ruby-comment"># =&gt; [&quot;xxxx&quot;, &quot;xxx&quot;, &quot;xx&quot;, &quot;x&quot;]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">0</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">sort_by</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span> } <span class="ruby-comment"># =&gt; [[:baz, 0], [:bar, 1], [:foo, 2]]</span>
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">sort_by</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span> }   <span class="ruby-comment"># =&gt; [[:bar, 1], [:baz, 0], [:foo, 2]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

<p>The current implementation of <a href="Enumerable.html#method-i-sort_by"><code>sort_by</code></a> generates an array of tuples containing the original collection element and the mapped value. This makes <a href="Enumerable.html#method-i-sort_by"><code>sort_by</code></a> fairly expensive when the keysets are simple.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;benchmark&#39;</span>

<span class="ruby-identifier">a</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">100000</span>).<span class="ruby-identifier">map</span> { <span class="ruby-identifier">rand</span>(<span class="ruby-value">100000</span>) }

<span class="ruby-constant">Benchmark</span>.<span class="ruby-identifier">bm</span>(<span class="ruby-value">10</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">b</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">b</span>.<span class="ruby-identifier">report</span>(<span class="ruby-string">&quot;Sort&quot;</span>)    { <span class="ruby-identifier">a</span>.<span class="ruby-identifier">sort</span> }
  <span class="ruby-identifier">b</span>.<span class="ruby-identifier">report</span>(<span class="ruby-string">&quot;Sort by&quot;</span>) { <span class="ruby-identifier">a</span>.<span class="ruby-identifier">sort_by</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span><span class="ruby-operator">|</span> <span class="ruby-identifier">a</span> } }
<span class="ruby-keyword">end</span>
</pre>

<p><em>produces:</em></p>

<pre>user     system      total        real
Sort        0.180000   0.000000   0.180000 (  0.175469)
Sort by     1.980000   0.040000   2.020000 (  2.013586)</pre>

<p>However, consider the case where comparing the keys is a non-trivial operation. The following code sorts some files on modification time using the basic <a href="Enumerable.html#method-i-sort"><code>sort</code></a> method.</p>

<pre class="ruby"><span class="ruby-identifier">files</span> = <span class="ruby-constant">Dir</span>[<span class="ruby-string">&quot;*&quot;</span>]
<span class="ruby-identifier">sorted</span> = <span class="ruby-identifier">files</span>.<span class="ruby-identifier">sort</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">a</span>).<span class="ruby-identifier">mtime</span> <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">b</span>).<span class="ruby-identifier">mtime</span> }
<span class="ruby-identifier">sorted</span>   <span class="ruby-comment">#=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]</span>
</pre>

<p>This sort is inefficient: it generates two new <a href="File.html"><code>File</code></a> objects during every comparison. A slightly better technique is to use the <a href="Kernel.html#method-i-test"><code>Kernel#test</code></a> method to generate the modification times directly.</p>

<pre class="ruby"><span class="ruby-identifier">files</span> = <span class="ruby-constant">Dir</span>[<span class="ruby-string">&quot;*&quot;</span>]
<span class="ruby-identifier">sorted</span> = <span class="ruby-identifier">files</span>.<span class="ruby-identifier">sort</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">test</span>(<span class="ruby-value">?M</span>, <span class="ruby-identifier">a</span>) <span class="ruby-operator">&lt;=&gt;</span> <span class="ruby-identifier">test</span>(<span class="ruby-value">?M</span>, <span class="ruby-identifier">b</span>)
}
<span class="ruby-identifier">sorted</span>   <span class="ruby-comment">#=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]</span>
</pre>

<p>This still generates many unnecessary <a href="Time.html"><code>Time</code></a> objects. A more efficient technique is to cache the sort keys (modification times in this case) before the sort. Perl users often call this approach a Schwartzian transform, after Randal Schwartz. We construct a temporary array, where each element is an array containing our sort key along with the filename. We sort this array, and then extract the filename from the result.</p>

<pre class="ruby"><span class="ruby-identifier">sorted</span> = <span class="ruby-constant">Dir</span>[<span class="ruby-string">&quot;*&quot;</span>].<span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span>
   [<span class="ruby-identifier">test</span>(<span class="ruby-value">?M</span>, <span class="ruby-identifier">f</span>), <span class="ruby-identifier">f</span>]
}.<span class="ruby-identifier">sort</span>.<span class="ruby-identifier">collect</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>[<span class="ruby-value">1</span>] }
<span class="ruby-identifier">sorted</span>   <span class="ruby-comment">#=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]</span>
</pre>

<p>This is exactly what <a href="Enumerable.html#method-i-sort_by"><code>sort_by</code></a> does internally.</p>

<pre class="ruby"><span class="ruby-identifier">sorted</span> = <span class="ruby-constant">Dir</span>[<span class="ruby-string">&quot;*&quot;</span>].<span class="ruby-identifier">sort_by</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">test</span>(<span class="ruby-value">?M</span>, <span class="ruby-identifier">f</span>) }
<span class="ruby-identifier">sorted</span>   <span class="ruby-comment">#=&gt; [&quot;mon&quot;, &quot;tues&quot;, &quot;wed&quot;, &quot;thurs&quot;]</span>
</pre>

<p>To produce the reverse of a specific order, the following can be used:</p>

<pre>ary.sort_by { ... }.reverse!</pre>

                              <div class="method-source-code" id="sort_by-source">
            <pre>static VALUE
enum_sort_by(VALUE obj)
{
    VALUE ary, buf;
    struct MEMO *memo;
    long i;
    struct sort_by_data *data;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    if (RB_TYPE_P(obj, T_ARRAY) &amp;&amp; RARRAY_LEN(obj) &lt;= LONG_MAX/2) {
        ary = rb_ary_new2(RARRAY_LEN(obj)*2);
    }
    else {
        ary = rb_ary_new();
    }
    RBASIC_CLEAR_CLASS(ary);
    buf = rb_ary_hidden_new(SORT_BY_BUFSIZE*2);
    rb_ary_store(buf, SORT_BY_BUFSIZE*2-1, Qnil);
    memo = MEMO_NEW(0, 0, 0);
    data = (struct sort_by_data *)&amp;memo-&gt;v1;
    RB_OBJ_WRITE(memo, &amp;data-&gt;ary, ary);
    RB_OBJ_WRITE(memo, &amp;data-&gt;buf, buf);
    data-&gt;n = 0;
    data-&gt;primitive_uniformed = SORT_BY_UNIFORMED((CMP_OPTIMIZABLE(FLOAT) &amp;&amp; CMP_OPTIMIZABLE(INTEGER)),
                                                  CMP_OPTIMIZABLE(FLOAT),
                                                  CMP_OPTIMIZABLE(INTEGER));
    rb_block_call(obj, id_each, 0, 0, sort_by_i, (VALUE)memo);
    ary = data-&gt;ary;
    buf = data-&gt;buf;
    if (data-&gt;n) {
        rb_ary_resize(buf, data-&gt;n*2);
        rb_ary_concat(ary, buf);
    }
    if (RARRAY_LEN(ary) &gt; 2) {
        if (data-&gt;primitive_uniformed) {
            RARRAY_PTR_USE(ary, ptr,
                           rb_uniform_intro_sort_2((struct rb_uniform_sort_data*)ptr,
                                                   (struct rb_uniform_sort_data*)(ptr + RARRAY_LEN(ary))));
        }
        else {
            RARRAY_PTR_USE(ary, ptr,
                           ruby_qsort(ptr, RARRAY_LEN(ary)/2, 2*sizeof(VALUE),
                                      sort_by_cmp, (void *)ary));
        }
    }
    if (RBASIC(ary)-&gt;klass) {
        rb_raise(rb_eRuntimeError, &quot;sort_by reentered&quot;);
    }
    for (i=1; i&lt;RARRAY_LEN(ary); i+=2) {
        RARRAY_ASET(ary, i/2, RARRAY_AREF(ary, i));
    }
    rb_ary_resize(ary, RARRAY_LEN(ary)/2);
    RBASIC_SET_CLASS_RAW(ary, rb_cArray);

    return ary;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-sum" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          sum(initial_value = 0)                  &rarr; number
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          sum(initial_value = 0) {|element| ... } &rarr; object
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With no block given, returns the sum of <code>initial_value</code> and the elements:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">100</span>).<span class="ruby-identifier">sum</span>          <span class="ruby-comment"># =&gt; 5050</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">100</span>).<span class="ruby-identifier">sum</span>(<span class="ruby-value">1</span>)       <span class="ruby-comment"># =&gt; 5051</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;d&#39;</span>).<span class="ruby-identifier">sum</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># =&gt; &quot;fooabcd&quot;</span>
</pre>

<p>Generally, the sum is computed using methods <code>+</code> and <code>each</code>; for performance optimizations, those methods may not be used, and so any redefinition of those methods may not have effect here.</p>

<p>One such optimization: When possible, computes using Gauss’s summation formula <em>n(n+1)/2</em>:</p>

<pre class="ruby"><span class="ruby-value">100</span> <span class="ruby-operator">*</span> (<span class="ruby-value">100</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>) <span class="ruby-operator">/</span> <span class="ruby-value">2</span> <span class="ruby-comment"># =&gt; 5050</span>
</pre>

<p>With a block given, calls the block with each element; returns the sum of <code>initial_value</code> and the block return values:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">sum</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span><span class="ruby-operator">*</span><span class="ruby-identifier">i</span> }                        <span class="ruby-comment"># =&gt; 30</span>
(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">sum</span>(<span class="ruby-value">100</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span><span class="ruby-operator">*</span><span class="ruby-identifier">i</span> }                   <span class="ruby-comment"># =&gt; 130</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">a:</span> <span class="ruby-value">0</span>, <span class="ruby-value">b:</span> <span class="ruby-value">1</span>, <span class="ruby-value">c:</span> <span class="ruby-value">2</span>, <span class="ruby-value">d:</span> <span class="ruby-value">3</span>, <span class="ruby-value">e:</span> <span class="ruby-value">4</span>, <span class="ruby-value">f:</span> <span class="ruby-value">5</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">sum</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">odd?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">value</span> <span class="ruby-operator">:</span> <span class="ruby-value">0</span> } <span class="ruby-comment"># =&gt; 9</span>
(<span class="ruby-string">&#39;a&#39;</span><span class="ruby-operator">..</span><span class="ruby-string">&#39;f&#39;</span>).<span class="ruby-identifier">sum</span>(<span class="ruby-string">&#39;x&#39;</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">&lt;</span> <span class="ruby-string">&#39;d&#39;</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">:</span> <span class="ruby-string">&#39;&#39;</span> }  <span class="ruby-comment"># =&gt; &quot;xabc&quot;</span>
</pre>

                              <div class="method-source-code" id="sum-source">
            <pre>static VALUE
enum_sum(int argc, VALUE* argv, VALUE obj)
{
    struct enum_sum_memo memo;
    VALUE beg, end;
    int excl;

    memo.v = (rb_check_arity(argc, 0, 1) == 0) ? LONG2FIX(0) : argv[0];
    memo.block_given = rb_block_given_p();
    memo.n = 0;
    memo.r = Qundef;

    if ((memo.float_value = RB_FLOAT_TYPE_P(memo.v))) {
        memo.f = RFLOAT_VALUE(memo.v);
        memo.c = 0.0;
    }
    else {
        memo.f = 0.0;
        memo.c = 0.0;
    }

    if (RTEST(rb_range_values(obj, &amp;beg, &amp;end, &amp;excl))) {
        if (!memo.block_given &amp;&amp; !memo.float_value &amp;&amp;
                (FIXNUM_P(beg) || RB_BIGNUM_TYPE_P(beg)) &amp;&amp;
                (FIXNUM_P(end) || RB_BIGNUM_TYPE_P(end))) {
            return int_range_sum(beg, end, excl, memo.v);
        }
    }

    if (RB_TYPE_P(obj, T_HASH) &amp;&amp;
            rb_method_basic_definition_p(CLASS_OF(obj), id_each))
        hash_sum(obj, &amp;memo);
    else
        rb_block_call(obj, id_each, 0, 0, enum_sum_i, (VALUE)&amp;memo);

    if (memo.float_value) {
        return DBL2NUM(memo.f + memo.c);
    }
    else {
        if (memo.n != 0)
            memo.v = rb_fix_plus(LONG2FIX(memo.n), memo.v);
        if (!UNDEF_P(memo.r)) {
            memo.v = rb_rational_plus(memo.r, memo.v);
        }
        return memo.v;
    }
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-take" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          take(n) &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>For non-negative integer <code>n</code>, returns the first <code>n</code> elements:</p>

<pre class="ruby"><span class="ruby-identifier">r</span> = (<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>)
<span class="ruby-identifier">r</span>.<span class="ruby-identifier">take</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># =&gt; [1, 2]</span>
<span class="ruby-identifier">r</span>.<span class="ruby-identifier">take</span>(<span class="ruby-value">0</span>) <span class="ruby-comment"># =&gt; []</span>

<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bat:</span> <span class="ruby-value">3</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">take</span>(<span class="ruby-value">2</span>) <span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1]]</span>
</pre>

                              <div class="method-source-code" id="take-source">
            <pre>static VALUE
enum_take(VALUE obj, VALUE n)
{
    struct MEMO *memo;
    VALUE result;
    long len = NUM2LONG(n);

    if (len &lt; 0) {
        rb_raise(rb_eArgError, &quot;attempt to take negative size&quot;);
    }

    if (len == 0) return rb_ary_new2(0);
    result = rb_ary_new2(len);
    memo = MEMO_NEW(result, 0, len);
    rb_block_call(obj, id_each, 0, 0, take_i, (VALUE)memo);
    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-take_while" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          take_while {|element| ... } &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          take_while                  &rarr; enumerator
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Calls the block with successive elements as long as the block returns a truthy value; returns an array of all elements up to that point:</p>

<pre class="ruby">(<span class="ruby-value">1</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">take_while</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">3</span> } <span class="ruby-comment"># =&gt; [1, 2]</span>
<span class="ruby-identifier">h</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">h</span>.<span class="ruby-identifier">take_while</span>{<span class="ruby-operator">|</span><span class="ruby-identifier">element</span><span class="ruby-operator">|</span> <span class="ruby-identifier">key</span>, <span class="ruby-identifier">value</span> = <span class="ruby-operator">*</span><span class="ruby-identifier">element</span>; <span class="ruby-identifier">value</span> <span class="ruby-operator">&lt;</span> <span class="ruby-value">2</span> }
<span class="ruby-comment"># =&gt; [[:foo, 0], [:bar, 1]]</span>
</pre>

<p>With no block given, returns an <a href="Enumerator.html"><code>Enumerator</code></a>.</p>

                              <div class="method-source-code" id="take_while-source">
            <pre>static VALUE
enum_take_while(VALUE obj)
{
    VALUE ary;

    RETURN_ENUMERATOR(obj, 0, 0);
    ary = rb_ary_new();
    rb_block_call(obj, id_each, 0, 0, take_while_i, ary);
    return ary;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-tally" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          tally &rarr; new_hash
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          tally(hash) &rarr; hash
                              </span>
                            </div>

                            <div class="method-description">
                              <p>Returns a hash containing the counts of equal elements:</p>
<ul><li>
<p>Each key is an element of <code>self</code>.</p>
</li><li>
<p>Each value is the number elements equal to that key.</p>
</li></ul>

<p>With no argument:</p>

<pre class="ruby"><span class="ruby-node">%w[a b c b c a c b]</span>.<span class="ruby-identifier">tally</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;2, &quot;b&quot;=&gt;3, &quot;c&quot;=&gt;3}</span>
</pre>

<p>With a hash argument, that hash is used for the tally (instead of a new hash), and is returned; this may be useful for accumulating tallies across multiple enumerables:</p>

<pre class="ruby"><span class="ruby-identifier">hash</span> = {}
<span class="ruby-identifier">hash</span> = <span class="ruby-node">%w[a c d b c a]</span>.<span class="ruby-identifier">tally</span>(<span class="ruby-identifier">hash</span>)
<span class="ruby-identifier">hash</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;2, &quot;c&quot;=&gt;2, &quot;d&quot;=&gt;1, &quot;b&quot;=&gt;1}</span>
<span class="ruby-identifier">hash</span> = <span class="ruby-node">%w[b a z]</span>.<span class="ruby-identifier">tally</span>(<span class="ruby-identifier">hash</span>)
<span class="ruby-identifier">hash</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;3, &quot;c&quot;=&gt;2, &quot;d&quot;=&gt;1, &quot;b&quot;=&gt;2, &quot;z&quot;=&gt;1}</span>
<span class="ruby-identifier">hash</span> = <span class="ruby-node">%w[b a m]</span>.<span class="ruby-identifier">tally</span>(<span class="ruby-identifier">hash</span>)
<span class="ruby-identifier">hash</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;4, &quot;c&quot;=&gt;2, &quot;d&quot;=&gt;1, &quot;b&quot;=&gt;3, &quot;z&quot;=&gt;1, &quot;m&quot;=&gt; 1}</span>
</pre>

                              <div class="method-source-code" id="tally-source">
            <pre>static VALUE
enum_tally(int argc, VALUE *argv, VALUE obj)
{
    VALUE hash;
    if (rb_check_arity(argc, 0, 1)) {
        hash = rb_to_hash_type(argv[0]);
        rb_check_frozen(hash);
    }
    else {
        hash = rb_hash_new();
    }

    return enum_hashify_into(obj, 0, 0, tally_i, hash);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-to_a" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          to_a(*args) &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns an array containing the items in <code>self</code>:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">4</span>).<span class="ruby-identifier">to_a</span> <span class="ruby-comment"># =&gt; [0, 1, 2, 3, 4]</span>
</pre>

                              <div class="method-source-code" id="to_a-source">
            <pre>static VALUE
enum_to_a(int argc, VALUE *argv, VALUE obj)
{
    VALUE ary = rb_ary_new();

    rb_block_call_kw(obj, id_each, argc, argv, collect_all, ary, RB_PASS_CALLED_KEYWORDS);

    return ary;
}</pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="Enumerable.html#method-i-entries">entries</a>
                            </div>

                          </div>

                  <div id="method-i-to_h" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          to_h(*args) &rarr; hash
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          to_h(*args) {|element| ... }  &rarr; hash
                              </span>
                            </div>

                            <div class="method-description">
                              <p>When <code>self</code> consists of 2-element arrays, returns a hash each of whose entries is the key-value pair formed from one of those arrays:</p>

<pre class="ruby">[[<span class="ruby-value">:foo</span>, <span class="ruby-value">0</span>], [<span class="ruby-value">:bar</span>, <span class="ruby-value">1</span>], [<span class="ruby-value">:baz</span>, <span class="ruby-value">2</span>]].<span class="ruby-identifier">to_h</span> <span class="ruby-comment"># =&gt; {:foo=&gt;0, :bar=&gt;1, :baz=&gt;2}</span>
</pre>

<p>When a block is given, the block is called with each element of <code>self</code>; the block should return a 2-element array which becomes a key-value pair in the returned hash:</p>

<pre class="ruby">(<span class="ruby-value">0</span><span class="ruby-operator">..</span><span class="ruby-value">3</span>).<span class="ruby-identifier">to_h</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">i</span>, <span class="ruby-identifier">i</span> <span class="ruby-operator">**</span> <span class="ruby-value">2</span>]} <span class="ruby-comment"># =&gt; {0=&gt;0, 1=&gt;1, 2=&gt;4, 3=&gt;9}</span>
</pre>

<p>Raises an exception if an element of <code>self</code> is not a 2-element array, and a block is not passed.</p>

                              <div class="method-source-code" id="to_h-source">
            <pre>static VALUE
enum_to_h(int argc, VALUE *argv, VALUE obj)
{
    rb_block_call_func *iter = rb_block_given_p() ? enum_to_h_ii : enum_to_h_i;
    return enum_hashify(obj, argc, argv, iter);
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-to_set" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">to_set</span><span
                                class="method-args">(klass = Set, *args, &amp;block)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Makes a set from the enumerable object with given arguments.</p>

                              <div class="method-source-code" id="to_set-source">
            <pre><span class="ruby-comment"># File ruby_3_3_0_preview2/prelude.rb, line 28</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_set</span>(<span class="ruby-identifier">klass</span> = <span class="ruby-constant">Set</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">klass</span>.<span class="ruby-identifier">new</span>(<span class="ruby-keyword">self</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-uniq" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          uniq                  &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          uniq {|element| ... } &rarr; array
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With no block, returns a new array containing only unique elements; the array has no two elements <code>e0</code> and <code>e1</code> such that <code>e0.eql?(e1)</code>:</p>

<pre class="ruby"><span class="ruby-node">%w[a b c c b a a b c]</span>.<span class="ruby-identifier">uniq</span>       <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;]</span>
[<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">2</span>, <span class="ruby-value">1</span>, <span class="ruby-value">0</span>, <span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>].<span class="ruby-identifier">uniq</span> <span class="ruby-comment"># =&gt; [0, 1, 2]</span>
</pre>

<p>With a block, returns a new array containing elements only for which the block returns a unique value:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>, <span class="ruby-value">3</span>, <span class="ruby-value">4</span>, <span class="ruby-value">5</span>, <span class="ruby-value">5</span>, <span class="ruby-value">4</span>, <span class="ruby-value">3</span>, <span class="ruby-value">2</span>, <span class="ruby-value">1</span>]
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">uniq</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">i</span><span class="ruby-operator">|</span> <span class="ruby-identifier">i</span>.<span class="ruby-identifier">even?</span> <span class="ruby-operator">?</span> <span class="ruby-identifier">i</span> <span class="ruby-operator">:</span> <span class="ruby-value">0</span> } <span class="ruby-comment"># =&gt; [0, 2, 4]</span>
<span class="ruby-identifier">a</span> = <span class="ruby-node">%w[a b c d e e d c b a a b c d e]</span>
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">uniq</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">c</span><span class="ruby-operator">|</span> <span class="ruby-identifier">c</span> <span class="ruby-operator">&lt;</span> <span class="ruby-string">&#39;c&#39;</span> }         <span class="ruby-comment"># =&gt; [&quot;a&quot;, &quot;c&quot;]</span>
</pre>

                              <div class="method-source-code" id="uniq-source">
            <pre>static VALUE
enum_uniq(VALUE obj)
{
    VALUE hash, ret;
    rb_block_call_func *const func =
        rb_block_given_p() ? uniq_iter : uniq_func;

    hash = rb_obj_hide(rb_hash_new());
    rb_block_call(obj, id_each, 0, 0, func, hash);
    ret = rb_hash_values(hash);
    rb_hash_clear(hash);
    return ret;
}</pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-zip" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          zip(*other_enums) &rarr; array
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>
                      <div class="method-heading">
                        <span class="method-callseq">
                          zip(*other_enums) {|array| ... } &rarr; nil
                              </span>
                            </div>

                            <div class="method-description">
                              <p>With no block given, returns a new array <code>new_array</code> of size self.size whose elements are arrays. Each nested array <code>new_array[n]</code> is of size <code>other_enums.size+1</code>, and contains:</p>
<ul><li>
<p>The <code>n</code>-th element of self.</p>
</li><li>
<p>The <code>n</code>-th element of each of the <code>other_enums</code>.</p>
</li></ul>

<p>If all <code>other_enums</code> and self are the same size, all elements are included in the result, and there is no <code>nil</code>-filling:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">:a0</span>, <span class="ruby-value">:a1</span>, <span class="ruby-value">:a2</span>, <span class="ruby-value">:a3</span>]
<span class="ruby-identifier">b</span> = [<span class="ruby-value">:b0</span>, <span class="ruby-value">:b1</span>, <span class="ruby-value">:b2</span>, <span class="ruby-value">:b3</span>]
<span class="ruby-identifier">c</span> = [<span class="ruby-value">:c0</span>, <span class="ruby-value">:c1</span>, <span class="ruby-value">:c2</span>, <span class="ruby-value">:c3</span>]
<span class="ruby-identifier">d</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">zip</span>(<span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>)
<span class="ruby-identifier">d</span> <span class="ruby-comment"># =&gt; [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]</span>

<span class="ruby-identifier">f</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">1</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">2</span>}
<span class="ruby-identifier">g</span> = {<span class="ruby-value">goo:</span> <span class="ruby-value">3</span>, <span class="ruby-value">gar:</span> <span class="ruby-value">4</span>, <span class="ruby-value">gaz:</span> <span class="ruby-value">5</span>}
<span class="ruby-identifier">h</span> = {<span class="ruby-value">hoo:</span> <span class="ruby-value">6</span>, <span class="ruby-value">har:</span> <span class="ruby-value">7</span>, <span class="ruby-value">haz:</span> <span class="ruby-value">8</span>}
<span class="ruby-identifier">d</span> = <span class="ruby-identifier">f</span>.<span class="ruby-identifier">zip</span>(<span class="ruby-identifier">g</span>, <span class="ruby-identifier">h</span>)
<span class="ruby-identifier">d</span> <span class="ruby-comment"># =&gt; [</span>
  <span class="ruby-comment">#      [[:foo, 0], [:goo, 3], [:hoo, 6]],</span>
  <span class="ruby-comment">#      [[:bar, 1], [:gar, 4], [:har, 7]],</span>
  <span class="ruby-comment">#      [[:baz, 2], [:gaz, 5], [:haz, 8]]</span>
  <span class="ruby-comment">#    ]</span>
</pre>

<p>If any enumerable in other_enums is smaller than self, fills to <code>self.size</code> with <code>nil</code>:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">:a0</span>, <span class="ruby-value">:a1</span>, <span class="ruby-value">:a2</span>, <span class="ruby-value">:a3</span>]
<span class="ruby-identifier">b</span> = [<span class="ruby-value">:b0</span>, <span class="ruby-value">:b1</span>, <span class="ruby-value">:b2</span>]
<span class="ruby-identifier">c</span> = [<span class="ruby-value">:c0</span>, <span class="ruby-value">:c1</span>]
<span class="ruby-identifier">d</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">zip</span>(<span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>)
<span class="ruby-identifier">d</span> <span class="ruby-comment"># =&gt; [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, nil], [:a3, nil, nil]]</span>
</pre>

<p>If any enumerable in other_enums is larger than self, its trailing elements are ignored:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">:a0</span>, <span class="ruby-value">:a1</span>, <span class="ruby-value">:a2</span>, <span class="ruby-value">:a3</span>]
<span class="ruby-identifier">b</span> = [<span class="ruby-value">:b0</span>, <span class="ruby-value">:b1</span>, <span class="ruby-value">:b2</span>, <span class="ruby-value">:b3</span>, <span class="ruby-value">:b4</span>]
<span class="ruby-identifier">c</span> = [<span class="ruby-value">:c0</span>, <span class="ruby-value">:c1</span>, <span class="ruby-value">:c2</span>, <span class="ruby-value">:c3</span>, <span class="ruby-value">:c4</span>, <span class="ruby-value">:c5</span>]
<span class="ruby-identifier">d</span> = <span class="ruby-identifier">a</span>.<span class="ruby-identifier">zip</span>(<span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>)
<span class="ruby-identifier">d</span> <span class="ruby-comment"># =&gt; [[:a0, :b0, :c0], [:a1, :b1, :c1], [:a2, :b2, :c2], [:a3, :b3, :c3]]</span>
</pre>

<p>When a block is given, calls the block with each of the sub-arrays (formed as above); returns nil:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = [<span class="ruby-value">:a0</span>, <span class="ruby-value">:a1</span>, <span class="ruby-value">:a2</span>, <span class="ruby-value">:a3</span>]
<span class="ruby-identifier">b</span> = [<span class="ruby-value">:b0</span>, <span class="ruby-value">:b1</span>, <span class="ruby-value">:b2</span>, <span class="ruby-value">:b3</span>]
<span class="ruby-identifier">c</span> = [<span class="ruby-value">:c0</span>, <span class="ruby-value">:c1</span>, <span class="ruby-value">:c2</span>, <span class="ruby-value">:c3</span>]
<span class="ruby-identifier">a</span>.<span class="ruby-identifier">zip</span>(<span class="ruby-identifier">b</span>, <span class="ruby-identifier">c</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">sub_array</span><span class="ruby-operator">|</span> <span class="ruby-identifier">p</span> <span class="ruby-identifier">sub_array</span>} <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>Output:</p>

<pre class="ruby">[<span class="ruby-value">:a0</span>, <span class="ruby-value">:b0</span>, <span class="ruby-value">:c0</span>]
[<span class="ruby-value">:a1</span>, <span class="ruby-value">:b1</span>, <span class="ruby-value">:c1</span>]
[<span class="ruby-value">:a2</span>, <span class="ruby-value">:b2</span>, <span class="ruby-value">:c2</span>]
[<span class="ruby-value">:a3</span>, <span class="ruby-value">:b3</span>, <span class="ruby-value">:c3</span>]
</pre>

                              <div class="method-source-code" id="zip-source">
            <pre>static VALUE
enum_zip(int argc, VALUE *argv, VALUE obj)
{
    int i;
    ID conv;
    struct MEMO *memo;
    VALUE result = Qnil;
    VALUE args = rb_ary_new4(argc, argv);
    int allary = TRUE;

    argv = RARRAY_PTR(args);
    for (i=0; i&lt;argc; i++) {
        VALUE ary = rb_check_array_type(argv[i]);
        if (NIL_P(ary)) {
            allary = FALSE;
            break;
        }
        argv[i] = ary;
    }
    if (!allary) {
        static const VALUE sym_each = STATIC_ID2SYM(id_each);
        CONST_ID(conv, &quot;to_enum&quot;);
        for (i=0; i&lt;argc; i++) {
            if (!rb_respond_to(argv[i], id_each)) {
                rb_raise(rb_eTypeError, &quot;wrong argument type %&quot;PRIsVALUE&quot; (must respond to :each)&quot;,
                         rb_obj_class(argv[i]));
            }
            argv[i] = rb_funcallv(argv[i], conv, 1, &amp;sym_each);
        }
    }
    if (!rb_block_given_p()) {
        result = rb_ary_new();
    }

    /* TODO: use NODE_DOT2 as memo(v, v, -) */
    memo = MEMO_NEW(result, args, 0);
    rb_block_call(obj, id_each, 0, 0, allary ? zip_ary : zip_i, (VALUE)memo);

    return result;
}</pre>
                              </div>
                            </div>


                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is provided by <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>. </p><p><a href="https://jamesbritt.bandcamp.com/">Hack your world.  Feed your head.  Live curious</a>.</p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

